Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP session destroyed / lost after header

I've got a script that sets some session values before redirecting to / using header().

I've read many posts about the $_SESSION variable being destroyed / lost after header(), even after I implemented this:

// set session here

session_regenerate_id(true);
session_write_close();
header("Location: /");

session_start() is set in the correct places, does anyone know of anything that I might be missing?

On the index.php page I have this:

session_start();
print_r($_SESSION);

// outputs nothing :'(

The code is pretty complex so will not post it all, just snippets.

like image 929
Ben Everard Avatar asked Jan 10 '10 14:01

Ben Everard


5 Answers

I've never seen any session related issues due to using location headers - are you sure you're calling session_start on both pages?


Hmm... this answer made a lot more sense before you added the session_start bits above, and mentioned the fact that you were sure you were using session_start. :-)

like image 181
John Parker Avatar answered Nov 16 '22 15:11

John Parker


header must be sent before session close

session_regenerate_id(true);

header("Location: /");
// the header must be sent before session close
session_write_close(); // here you could also use exit();
like image 43
streetparade Avatar answered Nov 16 '22 13:11

streetparade


just put exit; after header :D I solved by this

like image 4
junaid Avatar answered Nov 16 '22 15:11

junaid


After the Header redirect you need to exit the PHP script:

header("Location: /");
exit();
like image 4
vivek goyal Avatar answered Nov 16 '22 15:11

vivek goyal


In the interest of closing this question, we had concluded it was a problem with the server configuration, not surprising considering the host is well known for this kind of thing.

like image 3
Ben Everard Avatar answered Nov 16 '22 15:11

Ben Everard