grzegorczyk
asked on
header location $_SERVER["PHP_SELF"] loops, when stoped: Document contains no data
so I'm adding some stuff to the database, once done I try and redirect to the same page using:
header("location: ".$_SERVER['PHP_SELF']."?m sg=$action ");
exit();
but the code keeps on running. It loops and adds the same entry into the database over and over and over again. When I press stop I get a message "Document contains no data", when I go and check the database, the entry has been added many times. Code works fine if I hard code the page name. What am I doing wrong?
header("location: ".$_SERVER['PHP_SELF']."?m
exit();
but the code keeps on running. It loops and adds the same entry into the database over and over and over again. When I press stop I get a message "Document contains no data", when I go and check the database, the entry has been added many times. Code works fine if I hard code the page name. What am I doing wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The problem is that $_SERVER['PHP_SELF'] will return a '/index.php' filename. What you are looking for is 'index.php'
So a solution would be substr($_SERVER['PHP_SELF' ], 1); ... so
if (isset($_POST["season_subm it"])) {
// add stuff to the db...
header("location: " . substr($_SERVER['PHP_SELF' ], 1) . "?msg=$action");
exit();
}
So a solution would be substr($_SERVER['PHP_SELF'
if (isset($_POST["season_subm
// add stuff to the db...
header("location: " . substr($_SERVER['PHP_SELF'
exit();
}
ASKER
Hmm.. I get:
The directory name is invalid.
Which of course breaks it out of the loop. I also noticed that when its in the loop, even if I hit stop, the loop keeps on going, so if I refresh the page it continues to add entries to the database, until there is about 27 of them then it stops.
I'm really lost here...
The directory name is invalid.
Which of course breaks it out of the loop. I also noticed that when its in the loop, even if I hit stop, the loop keeps on going, so if I refresh the page it continues to add entries to the database, until there is about 27 of them then it stops.
I'm really lost here...
can u provide more of your code?
Like sint4x, I cannot by a glance find anything wrong. The test script I quickly made to test this here:
<?php
if (isset($_POST["submt"])) {
$fp=fopen("hi.txt","a");
fwrite($fp,"a");
header("Location: ".$_SERVER["PHP_SELF"]."?m sg=submt") ;
exit();
} else {
echo("<form action=\"".$_SERVER["PHP_S ELF"]."\" method=\"post\">");
echo("<input type='submit' name='submt'></form>");
}
?>
Is there any big difference between my script and your script? Does my script work for you? Please get back to us on this.
<?php
if (isset($_POST["submt"])) {
$fp=fopen("hi.txt","a");
fwrite($fp,"a");
header("Location: ".$_SERVER["PHP_SELF"]."?m
exit();
} else {
echo("<form action=\"".$_SERVER["PHP_S
echo("<input type='submit' name='submt'></form>");
}
?>
Is there any big difference between my script and your script? Does my script work for you? Please get back to us on this.
You might want to consider using a full url instead of a relative one, ie:
header('Location: http://'.$_SERVER['SERVER_NAME'].$_SERVER[' PHP_SELF'] .'?msg=sub mt');
Also try to test against the $_SERVER['REQUEST_METHOD'] , ie:
if ($_SERVER['REQUEST_METHOD' ] == 'POST'&& isset($_POST["submt"])) {
-r-
header('Location: http://'.$_SERVER['SERVER_NAME'].$_SERVER['
Also try to test against the $_SERVER['REQUEST_METHOD']
if ($_SERVER['REQUEST_METHOD'
-r-
ASKER
if (isset($_POST["season_subm
add stuff to the db...
header("location: ".$_SERVER['PHP_SELF']."?m
exit();
}
but I still get an infinite loop. Its weird because if I don't use PHP_SELF and use the actual page name it works fine...