IE Not Passing PHP Session Variables

5k Views Asked by At

I'm trying to use session variables across several pages. However, the variables disappear when I go from the original page they are set on, to another page on my server. I've confirmed the variables are set originally on the first page but when I try to access them on a subsequent page they have no value. If I access the session_id() it is different than the one I set on the first page.

Here's a simplified version of what I am trying to do: (First page where the session variables are set)

<?php
ini_set("display_errors", "1");
error_reporting(E_ALL); 
session_start();
$_SESSION['id'] = session_id();
header('Location: pagetwo.php');
die();
?>

(Second page where I try to access the session variables)

<?php
ini_set("display_errors", "1");
error_reporting(E_ALL); 
session_start();
echo "SESSION[id] = ".$_SESSION['id']."<br>";
?>

$_SESSION['id'] returns blank and I have an output error stating:

Notice: Undefined index: id in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\pagetwo.php on line 5

Now here is where things get a little weird. This does not happen in all browsers (only IE8 and Firefox 3). I am able to access the session variables in Chrome 15 and Firefox 7. Also, I have two different servers running Apache and PHP. The server where I am having these issues runs PHP 5.2.17, however, with my other server running PHP 5.2.9 I can access the session variables across all pages in all browser types.

It would seem like the PHP.ini files would be different between the two servers, however, they session settings are identical between them.

I'm not sure if someone else has run into a similar issue where IE8 could not access session variables in PHP 5.2.17 but I have searched the forms for the past two days and could not find anything else like this. I'm hoping I'm just missing something very simple and someone can point me in the right direction.

2

There are 2 best solutions below

0
On

I suspect it'd because you're not closing your session variable before redirecting your user to pagetwo.php. I'd recommend adding in a session_write_close() after setting the ID value in your code. It ensures the change is added to the session variable and is available for retrieval on a later page.

<?php 
session_start();
$_SESSION['id'] = session_id();
session_write_close();
header('Location: pagetwo.php'); 
?>

You can read more about session_write_close() on PHP.net and if you scroll down to the user contributed comments, you'll see a comment by a person named JP on this very issue.

Cheers!

0
On

I had the same problem while developing facebook applications - IE did not send the session cookie, thus every page request started a new one. The following header solved it for me:

header('P3P: CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"');