header + exit

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

header + exit

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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).
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 ;)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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....
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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)
Post Reply