Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts

    header(location) behavior-- code continues to execute?

    It appears to me, at least on my local xampp web server, that header("location:$url"); does not kill the script. PHP code further on down the line executes beyond the header() call before the header() call has a chance to kick in and redirect to another URL. I've always assumed it's just like calling die()-- any code after the header("location:") will never execute. But this apparently is not so!

    Can anyone explain this? I spent a couple of hours fiddling around before realizing this was happening. (And yes, I realize it's not good coding practice to end my script with header() but that's the nature of this particular beast)

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    It's a bugger the first time you realise that, ain't it. It wouldn't be much use if issuing a header call killed the script. exit() every time if you want processing to stop there and then.

  • #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,303
    Thanks
    4
    Thanked 204 Times in 201 Posts
    Header may be used to send all sorts of things and having it die or exit without warning would be a very bad thing. You are in control of what happens after you use header. Either exiting or doing other things.

    The best example of multiple uses of header that I can think of is when you want to deliver a file for download. It takes a sequence of header calls to properly tell the browser what is going on as illustrated below. If the use of header killed things automatically this, obviously, would not work.

    PHP Code:
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Content-disposition: attachment; filename=test.txt");
    header("Content-type: application/octetstream");
    header("Pragma: no-cache");
    header("Expires: 0"); 
    Dave .... HostMonster for all of your hosting needs

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Exactly Dave. What I like to do fumigator is a simple redirect function
    PHP Code:
    function redirect($url) {
        
    header('Location: '.$url);
        exit();

    This way it makes it as simple as redirect('http://www.google.com'); and you know it will redirect there immediately. It also means you won't forget to add the exit() like your original problem, as is easy to do in these situations
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •