Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php session variables lost between pages

Posted on 2012-04-04
17
Medium Priority
?
2,682 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 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 84

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 111

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 84

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
 

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 84

Accepted Solution

by:
Dave Baldwin earned 1800 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 84

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 84

Expert Comment

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

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 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 …
Suggested Courses

971 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