1

When I post to this script without specifying any POST variables, the script is suppose to redirect however, still saying. The funny thing i when I put die(); after the first if function the page actually gets redirected.

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$gender = $_POST['gender'];
$dob = $_POST['dob'];
$school = $_POST['school'];
$grade = $_POST['grade'];
$password = $_POST['password'];
$password2 = $_POST['password2'];


$test=FALSE;
 $required = array     ('firstname','lastname','email','phone','gender','dob','school','grade','password','password2');

foreach ($required as $value)
{
if(!isset($_POST[$value]) || empty($_POST[$value]))
{
        $test = TRUE;

}   
}

if($test)
{
header("location:../register.php?error=1");
}


$id = getRand(9);
$conn = getConnection();

$check =      saveUser($firstname,$lastname,$email,$phone,$gender,$dob,$school,$grade,$password,$id,$conn );


if($check)
{
header("location:../quiz.php");
}
else
{
header("location:../register.php");
}
Joel Dean
  • 2,444
  • 5
  • 32
  • 50
  • 1
    You should take a look at this question - http://stackoverflow.com/questions/3553698/php-should-i-call-exit-after-calling-location-header - You should stop the script execution after a redirect, either with exit; or die() – DrBeza Mar 16 '13 at 20:51

2 Answers2

1

You need to set a die() after the header(...) Command, or else everything after that will be executed. Do it like this:

if($test) {
    header("location:../register.php?error=1");
    die(); // Abort everything else
}

I don't understand your other question - you get redirected before the if ($test) ? Can you give me sample data for that?

Stefan
  • 2,028
  • 2
  • 36
  • 53
  • What I meant was that when I killed the script after the if function it worked.So I wanted to know if I had to kill the script after the redirecting. – Joel Dean Mar 16 '13 at 20:56
  • Yes, you have to kill it using `die()` or `exit()` – Stefan Mar 17 '13 at 09:08
1

Your header command does not redirect, but just sends a location header to the browser, and the script continues to run. (The browser then later follows this location header, after the script has run, and thus creates the redirect.)

However, if you send another location header after the first one (during the same script), this header overwrites the first one, and is then the only one that is sent to the browser.

You need to exit or die after the header command.

Wolfgang Stengel
  • 2,867
  • 1
  • 17
  • 22