Monthly Archives: February 2011

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.

session_write_close();
header("Location:/overview");
exit();

Posted in Something Interesting | 6 Comments