Page 1 of 1

simple PHP if statement question

Posted: Sat Sep 04, 2010 10:11 am
by taco_pants
I've simplified the following code just to get an understanding why the code is executed this way. This code will redirect to pagetwo.php instead of executing the if statement and redirecting to pageone.php. Why is this? I know it's easy to have pagetwo.php in an else statement and all will be fine but I'm curious why it wouldn't work this way also.

Code: Select all

$check = 1;

if ($check == 1)
{
  header("Location: ../pageone.php");
}

header("Location: ../pagetwo.php");

Re: simple PHP if statement question

Posted: Sat Sep 04, 2010 10:47 am
by josh
Have you taken a look at the headers that get output by that script? You can use the php socket functions to debug this kind of stuff. Most likely your script is outputting both Location: headers, the latter of which is being applied.

Re: simple PHP if statement question

Posted: Sat Sep 04, 2010 12:16 pm
by taco_pants
hmm, so if you don't put an exit(); after the redirect inside the if statement the code will continue to execute and send both headers, with the last one, pagetwo.php, being the page that is loaded since it was last.