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
alupkamanCTOAsked:
Who is Participating?
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
See if you can add 'Everyone' (or 'IUSR' if you are running IIS) with full permissions.  If this were 'production', you would want a separate directory that was used only by the PHP sessions.
0
 
Ray PaseurConnect With a Mentor Commented:
Instead of "echo print_r();" please try this:
var_dump($_SESSION);

Open in new window

I expect that the session is working correctly but the code is calling print_r() incorrectly.
0
 
Dave BaldwinFixer of ProblemsCommented:
Ray is right.  You can 'echo' or you can 'print_r' but "echo print_r($_SESSION);" should be an error.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Atique AnsariCommented:
Print session variables using

echo '<pre>';print_r($_SESSION);echo '</pre>';
0
 
Ray PaseurCommented:
... 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.
0
 
alupkamanCTOAuthor Commented:
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

0
 
alupkamanCTOAuthor Commented:
I guess my real question is why is $_SESSION['myName'] blank on page 2, line 1?
0
 
Dave BaldwinFixer of ProblemsCommented:
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

0
 
alupkamanCTOAuthor Commented:
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";
0
 
alupkamanCTOAuthor Commented:
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
0
 
alupkamanCTOAuthor Commented:
Why can it write the session file from test1, but not test2? How to fix?
0
 
alupkamanCTOAuthor Commented:
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.
0
 
alupkamanCTOAuthor Commented:
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?
0
 
alupkamanCTOAuthor Commented:
Thanks to Ray for the var_dump suggestion, and to Dave for solving the problem.
0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
 
alupkamanCTOAuthor Commented:
Excellent suggestion. Thank you.
0
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome, glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.