Link to home
Start Free TrialLog in
Avatar of grzegorczyk
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']."?msg=$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?
ASKER CERTIFIED SOLUTION
Avatar of minichicken
minichicken

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of grzegorczyk
grzegorczyk

ASKER

I do have that

if (isset($_POST["season_submit"])) {

add stuff to the db...

header("location: ".$_SERVER['PHP_SELF']."?msg=$action");
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...
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_submit"])) {

// add stuff to the db...

   header("location: " . substr($_SERVER['PHP_SELF'], 1) . "?msg=$action");
   exit();
}
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...
can u provide more of your code?
Avatar of Zyloch
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"]."?msg=submt");
   exit();
} else {
   echo("<form action=\"".$_SERVER["PHP_SELF"]."\" 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.
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=submt');


Also try to test against the $_SERVER['REQUEST_METHOD'], ie:

if ($_SERVER['REQUEST_METHOD'] == 'POST'&& isset($_POST["submt"])) {

-r-