PHP: session isn't saving before header redirect

phpNutt picture phpNutt · Jun 3, 2010 · Viewed 15.5k times · Source

I have read through the php manual for this problem and it seems quite a common issue but i have yet to find a solution. I am saving sessions in a database. My code is as follows:

// session
$_SESSION['userID'] = $user->id;        
header('Location: /subdirectory/index.php');

Then at the top of index.php after the session_start(), i have var_dumped the $_SESSION global and the userID is not in there. As i said ive looked through the PHP manual (http://php.net/manual/en/function.session-write-close.php) and neither session_write_close or session_regenerate_id(true) worked for me. Does anybody know a solution?

Edit: I have session_start() at the top of my file. When i var_dump the session global before the header redirect, i see the userID in there, but not in the other file, which is in a subdirectory of this script

Answer

Tim Van Samang picture Tim Van Samang · Apr 1, 2011

I know this is an old toppic but I found the solution (for me). I've put a exit after the header.

$_SESSION['session'] = 'this is a session';
header('location: apage.php');
exit;

This works for me