Solved

php session variables lost between pages

Posted on 2012-04-04
17
1,868 Views
Last Modified: 2012-04-05
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
0
Comment
Question by:alupkaman
  • 9
  • 5
  • 2
  • +1
17 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 50 total points
ID: 37809586
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
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37809618
Ray is right.  You can 'echo' or you can 'print_r' but "echo print_r($_SESSION);" should be an error.
0
 
LVL 7

Expert Comment

by:Atique Ansari
ID: 37809666
Print session variables using

echo '<pre>';print_r($_SESSION);echo '</pre>';
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37809703
... 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
 

Author Comment

by:alupkaman
ID: 37812613
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
 

Author Comment

by:alupkaman
ID: 37812661
I guess my real question is why is $_SESSION['myName'] blank on page 2, line 1?
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37812800
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
 

Author Comment

by:alupkaman
ID: 37812845
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:alupkaman
ID: 37812858
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
 

Author Comment

by:alupkaman
ID: 37812910
Why can it write the session file from test1, but not test2? How to fix?
0
 

Author Comment

by:alupkaman
ID: 37812949
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
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 450 total points
ID: 37812960
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
 

Author Comment

by:alupkaman
ID: 37813010
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
 

Author Closing Comment

by:alupkaman
ID: 37813043
Thanks to Ray for the var_dump suggestion, and to Dave for solving the problem.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37813077
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
 

Author Comment

by:alupkaman
ID: 37813167
Excellent suggestion. Thank you.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37813870
You're welcome, glad to help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now