Link to home
Start Free TrialLog in
Avatar of alupkaman
alupkamanFlag for China

asked on

php session variables lost between pages

Can anyone tell me why my Session variables are not persisting between pages?

test1.php:
<?php 
session_start(); 
$_SESSION['myName'] = "Alupka";
echo $_SESSION['myName'];
echo "<br>" . session_id();
echo "<br><pre>";
echo print_r($_SESSION);
echo "</pre>";
?>
<br>
<a href='/test2.php'>TEST 2</a>

Open in new window

test2.php:
<?php 
session_start(); 
echo $_SESSION['myName'];
echo session_id();
echo "<br>";
echo "<br><pre>";
echo print_r($_SESSION);
echo "</pre>";
?>
<br>
<a href='/test1.php'>TEST 1</a>

Open in new window

On first page, session variable prints as expected. On second page, session variable is blank, and the session array is completely empty, but session ID is the same.

Output for test1.php:
Alupka
a7edifqptt22c1ubntl9ine9v4

Array
(
    [myName] => Alupka
)
1

TEST 2

Open in new window


output for test2.php:
a7edifqptt22c1ubntl9ine9v4

Array
(
)
1


TEST 1

Open in new window

Problem is the same in both FF and IE8

My environment: running test app on localhost (Win 7 Pro) with IIS 7 and PHP installed
session save path is C:\windows\temp. All configuration settings (php.ini, etc) are default as far as I know. phpinfo() says sessions are enabled via cookies

Any help from the experts will be appreciated.

Thanks,
Alupka
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dave Baldwin
Ray is right.  You can 'echo' or you can 'print_r' but "echo print_r($_SESSION);" should be an error.
Print session variables using

echo '<pre>';print_r($_SESSION);echo '</pre>';
... but "echo print_r($_SESSION);" should be an error.
Couldn't agree more, however it's not an error.  Instead, have a look at the man page to see what PHP will do with that.
http://php.net/manual/en/function.print-r.php

Given the way the script called print_r() the return value was TRUE.  When you echo TRUE, you get "1" and that is exactly what the script produced.  Personally I like var_dump() better than print_r() and if you compare the outputs you will probably see why.  Var_dump() gives you more information.
Avatar of alupkaman

ASKER

Hi guys, thanks for the replies, but the problem still persists. I've fixed var_dump, but still having problems. Actually, printing the array is not part of the app, I just wanted to see if it contained anything. If you notice in the output (in line 1) the variable is completely blank on the second page, but works on the first. Here's updated code and output:

test1.php:
<?php 
session_start(); 
$_SESSION['myName'] = "Alupka";
echo $_SESSION['myName'];
echo "<br>" . session_id();
echo "<br><pre>";
var_dump($_SESSION);
echo "</pre>";
?>
<br>
<a href='/test2.php'>TEST 2</a>

Open in new window

test1 output
Alupka
a7edifqptt22c1ubntl9ine9v4

array(1) {
  ["myName"]=>
  string(6) "Alupka"
}

TEST 2

Open in new window


test2.php
<?php 
session_start(); 
echo $_SESSION['myName'];
echo session_id();
echo "<br>";
echo "<br><pre>";
var_dump($_SESSION);
echo "</pre>";
?>
<br>
<a href='/test1.php'>TEST 1</a>

Open in new window

test2 output
a7edifqptt22c1ubntl9ine9v4

array(0) {
}

TEST 1

Open in new window

I guess my real question is why is $_SESSION['myName'] blank on page 2, line 1?
I just copied your code and it works fine on my machine.  That makes me think that maybe your 'session.save_path' is not "writeable".  If nothing can actually written in that directory, your $_SESSION variables won't be saved.  'test1.php' still works because the variable is assigned there and still in memory.

Run phpinfo.php and see what your 'session.save_path' is and find out what the permissions are for it.

<?php
// this is phpinfo.php
phpinfo();
?>

Open in new window

session.save_path is       C:\Windows\temp

I deleted everything in that directory. When I refreshed test1.php a file is added to the temp folder called sess_a7edifqptt22c1ubntl9ine9v4. Opening the file in notepad has:

myName|s:6:"Alupka";
OK, looks like you're pointing me in the right direction.

When I clicked on the link to go to test2.php, it added another file in the temp directory called php53_errors.log, which says:

[05-Apr-2012 11:35:19] PHP Warning:  session_start(): open(C:\Windows\temp\sess_a7edifqptt22c1ubntl9ine9v4, O_RDWR) failed: Permission denied (13) in D:\ravensix\Client Sites\Macs Performance\devroot\test2.php on line 2
Why can it write the session file from test1, but not test2? How to fix?
Under Security tab for temp directory, SYSTEM and Administrators have full control, CREATOR OWNER and Users have nothing checked. No other groups or user names are listed.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you. adding IUSR with full permissions to temp folder solved the problem. I guess it was able to write but not modify, but still not sure why temp1.php was able to successfully write to the directory if IUSR was not listed at all.

Do you have a recommendation for where to store the session files in production environment?
Thanks to Ray for the var_dump suggestion, and to Dave for solving the problem.
For my PHP installations, it has always been 'C:\php\save' which is in the directory where my PHP files are.  Your installation may be different.
Excellent suggestion. Thank you.
You're welcome, glad to help.