Session Variables Not Being Preserved

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
corwin
Forum Newbie
Posts: 2
Joined: Thu Mar 19, 2009 1:35 pm

Session Variables Not Being Preserved

Post by corwin »

I am unable to use session variables on a page other than the one where they are set, IOW they act like non-session variables. I have found a similar question posted in half a dozen other similar fora, but the answer in those other cases always turns out not to apply.

Here are my files:

sess1.php

Code: Select all

<?php
session_start();
    
session_register("userid");
session_register("textvar");
   
$_SESSION['userid'] = 10333 ;
$_SESSION['textvar'] = TextVariable ;
    
echo "<p>User ID is: " . $_SESSION['userid'] . "</p>" ;
echo "<p>Another variable is: " . $_SESSION['textvar'] . "</p>" ;
?>
<p>Go to the <a href="sess2.php">next page</a>.</p>
and,
sess2.php

Code: Select all

<?php
session_start();
    
echo "<p>The userid session variable is: " . $_SESSION['userid'] . "</p>";
echo "<p>The other session variable is: " . $_SESSION['newvar']. "</p> ";
?>
The browser output in each case is:

sess1.php

Code: Select all

User ID is: 10333
 
Another variable is: TextVariable
 
Go to the [next page].
 
and,
sess2.php

Code: Select all

The userid session variable is:
 
The other session variable is:
 
Go to the [last page].
A few things it is NOT:
  • I do have session_start() at the top of both files.
  • The variables directory is writable, and the session variables are showing up there. (I have about a hundred little files called sess_b62<snip>, that have this inside: 'userid|i:10333;textvar|s:12:"TextVariable";'.)
  • phpinfo() tells me that the php.ini file is being read correctly and the lifetime is set to the default, 0, i.e. until the browser is closed.
I'm at my wit's end. Any suggestions?

Thanks so much.
atonalpanic
Forum Commoner
Posts: 29
Joined: Mon Mar 02, 2009 10:20 pm

Re: Session Variables Not Being Preserved

Post by atonalpanic »

Make sure your browser accepts cookies?
rcastera
Forum Newbie
Posts: 4
Joined: Wed Mar 11, 2009 8:07 pm

Re: Session Variables Not Being Preserved

Post by rcastera »

Hey,

Try removing line 4 and 5 in sess1.php and give it a shot. And don't forget atonalpanic's recommendation of making sure that your browser is accepting cookies.

http://www.php.net/session_register

Cheers!
corwin
Forum Newbie
Posts: 2
Joined: Thu Mar 19, 2009 1:35 pm

Re: Session Variables Not Being Preserved

Post by corwin »

Okay, I've discovered that the session IDs returned on each page are different, so for some reason the session ID isn't being retained from page to page. Great. Now what?
Post Reply