Solved

php session variables lost between pages

Posted on 2012-04-04
17
1,789 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
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
Print session variables using

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

Expert Comment

by:Ray Paseur
Comment Utility
... 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
Comment Utility
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
Comment Utility
I guess my real question is why is $_SESSION['myName'] blank on page 2, line 1?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:alupkaman
Comment Utility
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
Comment Utility
Why can it write the session file from test1, but not test2? How to fix?
0
 

Author Comment

by:alupkaman
Comment Utility
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 82

Accepted Solution

by:
Dave Baldwin earned 450 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks to Ray for the var_dump suggestion, and to Dave for solving the problem.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
Excellent suggestion. Thank you.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You're welcome, glad to help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

744 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

11 Experts available now in Live!

Get 1:1 Help Now