Page 1 of 1

header + exit

Posted: Sun Jan 01, 2006 8:53 pm
by alex.barylski
I occasionally use header redirects in my code...

and I've always got into the habit of putting an exit instruction right after incase for whatever reason header failed and the scirpt continues to execute, carrying out tasks which should NOT be carried out...

Never could think of anything to go wrong...just had it there for the sake of sleeping better at night...

Well I'm working on a project right now where I use this technique...and I couldn't figure out why my code was executing...so I looked closer and upon inspection I noticed I was missing an exit;

So I added it and problem solved...??? :?

Anyone know of a reason why header might fail like that???

I'm sure the header is correctly formatted, cuz the browser gets redirected properly and it's not an overly complex header, so i'm sure it's correct...but it wasn't redirecting until I used exit???

Just curious :)

Posted: Sun Jan 01, 2006 8:57 pm
by Chris Corbyn
I'm not sure if it's actually the browser that causes this rather than PHP. If the headers are sent and there is no exit() then PHP will still send the rest (AFAIK)... it's up to the browser to act upon the info in the HTTP headers.

So if your browser sees some content coming through after the headers it may very well just parse it. Using exit() is good practise. It seems to be more prominent in large scripts that take some time to process from what I've seen (PDF creation etc).

Posted: Sun Jan 01, 2006 8:59 pm
by John Cartwright
The entire php script must be parsed before any output is sent (headers in this case), so the rest of the script is executed. :wink:

In your case, your whole script is parsed and THEN output is outputted.

Code: Select all

session_start();

if (isset($_GET['redir'])) {
   header('Location: index.php');
   $_SESSION['somevar'] = true;
}

  print_r($_SESSION);
Try it for yourself ;)

Posted: Sun Jan 01, 2006 9:17 pm
by alex.barylski
Hmmmm...

Thanks for the feed back guys

I'm sure ive written other scripts in the past which didn't use exit and everythign worked normally...but maybe the headers where at the end of the script with nothing else to parse, hence it appeared to work as expected...

Regardless...

I don't feel so bad for always using exit after a header redirect :)

Cheers :)

Posted: Sun Jan 01, 2006 9:21 pm
by Chris Corbyn
Hockey wrote:Hmmmm...

Thanks for the feed back guys

I'm sure ive written other scripts in the past which didn't use exit and everythign worked normally...but maybe the headers where at the end of the script with nothing else to parse, hence it appeared to work as expected...

Regardless...

I don't feel so bad for always using exit after a header redirect :)

Cheers :)
You probably have.... that's why my reply suggested that it's not consistent :) I wrote a reporting class which generates PDF reports and it's very clear in that script that sometimes the headers work and sometimes they don't. Using exit( ) cleared that up though. In my case you could refresh the page and see a PDF, then refresh the exact same page again and see jibberish, then refresh again and see the PDF and so on....

Posted: Sun Jan 01, 2006 9:28 pm
by alex.barylski
d11wtq wrote:
Hockey wrote:Hmmmm...

Thanks for the feed back guys

I'm sure ive written other scripts in the past which didn't use exit and everythign worked normally...but maybe the headers where at the end of the script with nothing else to parse, hence it appeared to work as expected...

Regardless...

I don't feel so bad for always using exit after a header redirect :)

Cheers :)
You probably have.... that's why my reply suggested that it's not consistent :) I wrote a reporting class which generates PDF reports and it's very clear in that script that sometimes the headers work and sometimes they don't. Using exit( ) cleared that up though. In my case you could refresh the page and see a PDF, then refresh the exact same page again and see jibberish, then refresh again and see the PDF and so on....
For the hell of it I just looked at some code...and whoa!!!

I was missing exits...sometimes the damn code worked and others not...had me stumped until I just added the exit's

Works as expected now :)

Sweeeeeeeeet 8)