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
<?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
<?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
User ID is: 10333
Another variable is: TextVariable
Go to the [next page].
and, sess2.php
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, 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.
The session ID has to be carried along in some way in order that the same session can be used over several pages. In general this is done with a cookie (see
session.use_cookies) but it can also be done in the URL or in forms (seesession.use_trans_sid).So first you have to make sure that the session ID is transmitted so that PHP can load the right session and session data.
See also Is my understanding of PHP sessions correct?