Chrome not redirecting with PHP header(“Location”) redirect

Our good friend Google Chrome wasn’t cooperating tonight. It wouldn’t let me sign in on an site I’m working on. Turns out that’s because I set some login values into the $_SESSION variable and then do a header redirect. The values were not getting saved so the redirect would send me back to the login page (since $_SESSION didn’t say I was logged in!).

Apparently other browsers wait long enough after getting the redirect header that the server can save back the values.

The trick was to do a session_write_close() immediately before redirecting. This forces the variables to be saved before sending the user on to the next page.

Here’s my new standard 3 line redirect, which is working very well.


This entry was posted in Something Interesting. Bookmark the permalink.

6 Responses to Chrome not redirecting with PHP header(“Location”) redirect

  1. I had the exact same error and your solution seems to work. This is pretty bizarre! :) Thanks

  2. Michael says:

    Hi got the same problem, but not you’re excellent php skills.

    Where excactly should I put the session write close?

    return $isCrawler;

    if(!isBot($_SERVER[‘HTTP_USER_AGENT’]) AND $_SESSION[“over18”] != 1){
    header(“Location: verify.php?redirect=” . $PHP_SELF);

Leave a Reply

Your email address will not be published. Required fields are marked *