[nycphp-talk] Disappearing $_SESSION variables after using header ()

Tim Lieberman tim_lists at
Mon Nov 24 23:47:28 EST 2008

On Nov 24, 2008, at 11:01 PM, Michael Southwell wrote:

> Tony Furnivall wrote:
>> Many thanks to  [csnyder] and [Tim Lieberman] for their helpful  
>> suggestions about header ().
>> I now have that part of things working fine. However, because there  
>> is an implicit exit; after issuing the call to header (), I'm  
>> uncertain if the $_SESSION variables are being set properly.
>> When I trace them at the end of one page they are all there  
>> ($_SESSION['userid'] = $userid; $_SESSION['badgename']= 
>> $badgename;), but when I examine the $_SESSION array at the start  
>> of the next page, the variables do not exist. My guess is that by  
>> short-circuiting any nrmal end-of-script processing, I may be  
>> bypassing the write of the $_SESSION array to the temporary file.
> They've already been written; what you're doing is losing the  
> identification of the session, so they can't be found. So when you  
> use the header command, you need to carry along the session id as a  
> GET variable, like this:
> header( 'Location:somepage.php?PHPSESSID=' . session_id() );

That shouldn't be necessary.  I know I saw some problem like that, but  
the following sort of thing produces expected output for me:

[begin: pageone.php]
$_SESSION['foo'] = 'bar';
header("Location: pagetwo.php');

[end: pagetwo.php]
[begin pagetwo.php]
echo 'Foo: ' . $_SESSION['foo'];

[end: pagetwo.php]

visiting pageone.php results in a redirect to pagetwo.php, which  
proceeds to output:

Foo: bar

Tony -- is it possible you're not calling session_start() on your  
destination page?

If you redirect to some static page after setting the session var(s),  
and look at your cookies, do you have a PHPSESSID session cookie in  
your browser? (obviously, you'll want to clear any such cookies before  

But like I said, the funny thing is that I remember years ago dealing  
with some issue just like this ... just failing to recall specifics.

I know I do the above sort of thing all the time without incident.


More information about the talk mailing list