header() & exit(). BFFs

h

The PHP header(“Location: /”) function sends a redirect header to the browser. So thing about PHP is that it only sends headers to the browser once the page is parsed on the server. header(); will not stop the execution of code that comes after it. The script will continue to execute until it reaches the end. At which point it will then redirect.

Take this example

//Do stuff
//redirect to another page
if($user_loggedin){
    header("Location:  /login.php");
}
$_SESSION['page_views'] += 1;
/*
Display this page's content
*/

Although its quite clear to a human that if this user is not logged in, we should redirect to login.php, this script will actually continue to execute to the end and then redirect. which means although the page is never really viewed. it will still increment the $_SESSION[‘page_views’] variable.

So the headers are only sent back to the browser once PHP has completed rendering/building the page. At which point the browser redirects to the login.php. So to get the desired effect all you have to do is add a exit() function to stop script execution and force PHP to send the page to the browser as is.

So the code will now look like this:

//Do stuff
//redirect to another page
if($user_loggedin){
    header("Location:  /login.php");
    //stop executing the script
    exit();
}
$_SESSION['page_views'] += 1;
/*
Display this page's content
*/

What I do is just write a function to do both steps like this:

function redirect($url){
        header("Location: $url");
        exit();
}

usage:

redirect("/login.php");

About the author

Talifhani Luvhengo

A collection of particles named Tali Luvhengo

6 comments

  • This was posted on the 28th of May. That’s rather a long time ago. What have you been doing since then, slacker?

  • hi
    can you explain what is the header exactly and what it do and for what we should use it… why you use it in php not javascript or jquery ?

    best regards.

    • Hi Samir, The header() function is used to send raw HTTP headers to a client(Browser in most cases).

      Header. The most commonly used header i guess would be the redirect header. So if you wanted to redirect to google.com in your script, this would achieve that:

          header("Location: http://www.google.com");
      

      So that will send a redirect header to the client(Browser). So the browser will see that and redirect to google.com. You can also send other headers, like a 404(page not found), I’m sure you’ve seen this before. And tons of others like setting the content type of a page. Read up on it some more.

  • hi
    i saw in a tutorial another use for header redirect ,
    http://tutorialzine.com/2009/10/cool-login-system-php-jquery/
    they say there they use it to prevent posting same data twise to the script if the user refresh the page so
    they redirect him to clean url of the same page
    here is what they say exactly…

    “You may have noticed on some sites, that when you submit a form and later refresh the page, the data is sent all over again. This could become problematic as it could lead to a double registrations and unnecessary server load.

    We use the header function to prevent this, by redirecting the browser to the same page. This starts a fresh view of the page, without the browser associating it with a form submit. The result is that, on page refresh, no data is sent.

    do you know a better way to prevent posting same data twise ?

    another question please :
    when i typed my reply i mistyped my email and after i submitted the form it took me to an error page telling me
    that i typed a wrong email , but when i pressed on the browser back arrow button it took me to the comments page but all my reply data was empty , my question is there a way to fix this on php ?
    can you store the typed data in a session variables so the used never lose what he typed in this case ?
    or you have a better solution ?
    do you have a serverside solution ?

    best regards.

    • Hey Samir,

      I think that’s probably the best way to do it, I can think of any other way. That’s the same trick I use.

      On your second question. A session would probably be your best bet. So you would save the data in the session when the form is posted and only clear it when the post is successfully saved the database. Then you can restore the data to the for if its not successful with something like

      <input type=”text” name=”email” value=”<?php echo $_SESSION[‘posted_email’]; ?>” />

      Hope that makes sense.

Categories

Page optimized by WP Minify WordPress Plugin