• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5125
  • Last Modified:

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?
0
grzegorczyk
Asked:
grzegorczyk
1 Solution
 
minichickenCommented:
I think you need to check if data is posted to create the database entry. So you need an IF statement around the redirect.
Something like this:

<?php

if (isset($_POST['my_data'])) //you should change this to suit your situation.
{
header("location: ".$_SERVER['PHP_SELF']."?msg=$action");
exit();
}
?>

So if data exists for database entry then redirect otherwise continue loading the page and it wont get into an infinite loop.

Hope this help.
0
 
grzegorczykAuthor Commented:
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...
0
 
sint4xCommented:
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();
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
grzegorczykAuthor Commented:
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...
0
 
sint4xCommented:
can u provide more of your code?
0
 
ZylochCommented:
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.
0
 
RoonaanCommented:
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-
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now