views:

955

answers:

7

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.

+7  A: 

session_register() is not required and may be causing a problem here. Read the docs on session_register() - it is intended to assign session variables using existing variables.

and from here:

Well, session_register() tells PHP that a certain global variable should be considered a session variable. That means that at the end of the script's execution (which is when session data writes usually occur), the resulting value of that global variable will be written using the current enabled session handlers.

I think this is the problem that you are experiencing. At the end of the script execution the session variable gets over-written.

jonstjohn
Yeah, session_register() has been deprecated for quite a while, it definitely shouldn't be used any more. I don't know whether it's causing this specific issue, but it shouldn't be in that code regardless.
Chad Birch
Interesting. I'm working from an introductory PHP/SQL book that's copyright '07, and the code is pretty much a direct quote. Unfortunately, remvoing the session_register() lines doesn't seem to solve the problem. Thanks very much for the information.
A: 

Use some kind of tool and check the http headers so you can see how the cookie is being sent. Perhaps your web-server is miss-configured and sending out cookies with an invalid different domain.

You might want to look at doing session_set_cookie_params() on each request and making sure the correct domain, path, and such are set.

Zoredache
A: 

Use session_register in both files and it should work.

Milan Babuškov
Thanks for the suggestion. I tried it both ways, with the session_register() lines in both files, and without. No change to the behaviour appeared.
session_register is deprecated: don't use it if you can avoid it
Jason Rhodes
@Jason: care to share some URL where it says that it is deprecated? What should be used instead?
Milan Babuškov
+2  A: 

One mistake that I see is that in the first file you are setting $_SESSION['textvar'] and in the second file you are calling $_SESSION['newvar'].

Also, I tested your code on a server I know is working and it worked fine other than the above error.

I also tried removing the session_register() and the code still works perfectly.

Darryl Hein
Good eye - "newvar" was a typo, left over from when I copied the code into new simpler files. But the session_register() solution doesn't work on my system. Thanks very much for the suggestion.
Have you tried clearing your cookies (or deleting this sites cookie) and accessing this page as the first page you access again? Also, are you sure there is no other text above or the <?php ?
Darryl Hein
+2  A: 

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 (see session.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?

Gumbo
Thanks for the help. phpinfo() would seem to confirm that I'm using cookies and the ID is being transmitted. session.use_cookies is "On" and session.use_trans_sid is 0. Set-Cookie records the cookie that was created by the first page, and that page is named as the referer.
And the session ID is the same on both pages? That a Set-Cookie header field was sent doesn’t mean that the cookie has been accepted and sent along with the second request. Try to compare the `session_id()` return values in both pages.
Gumbo
Crud. The session IDs are different. Now what...?
Then I assume that you browser doesn’t accept that cookie. Have you already tried an other browser?
Gumbo
+1  A: 

*"session_register() accepts a variable number of arguments, any of which can be either a string holding the name of a variable or an array consisting of variable names or other arrays. For each name, session_register() registers the global variable with that name in the current session."*

because you have no variables with those names, the result will unpredictable.

just use $_SESSION[$key] = $value;

jcinacio
+1  A: 

If all the above does not solve the problem, I´ll just ask the obvious: There wouldn´t be any spaces or new-lines before the opening php tag?

You can also check for messages in the server error log file, that should tell you whether your variables are defined (although I guess that depends on the level of error reporting as well).

jeroen