Forum Moderators: coopster

Message Too Old, No Replies

sessions var problem

var not staying defined.

         

firemaster

11:32 pm on Apr 3, 2007 (gmt 0)

10+ Year Member



I'm trying to use some pretty basic session code. The problem is that the code works until I go to a new page or refresh.


<?php
session_start();
if (session_is_registered("gx_user"))
{
echo "Welcome,".$gx_user."<br />"; // RESULT A
}
else
{
// blah blah stuff RESULT B
}
?>

Like i said when the information is first entered it displays fine, but once it is refreshed it will still give result A, as desired, expect it just doesn't keep the var. I turn on error_reporting(E_ALL); and it says:

Notice: Undefined variable: gx_user in /home/.hostname/blah_user_blah/www.example.com/index.php on line 131

thanks a lot
-Mike

eelixduppy

11:46 pm on Apr 3, 2007 (gmt 0)



Try something like this:

<?php
session_start();
if (isset($_SESSION["gx_user"]))
{
echo "Welcome,".$_SESSION["gx_user"]."<br />"; // RESULT A
} else {
echo 'blah blah stuff RESULT B';
}
?>

firemaster

12:48 am on Apr 4, 2007 (gmt 0)

10+ Year Member



That works if the session is already registered, but if it's not and I try to register it gives me result B instead of result A and the form doesn't work at all. If need I can post the entire code.

capulet_x

1:32 am on Apr 4, 2007 (gmt 0)

10+ Year Member



Firemaster,
When you set $_SESSION["gx_user"] on the previous page are there conditions to the session like an expiration time? Also is the header on each page still www.yoursite.com or are they different. I think I had read somwhere that there is a URL association when you set cookies not sure if the same thing applies to sessions.

firemaster

4:46 am on Apr 4, 2007 (gmt 0)

10+ Year Member



I changed the way I had my code.. ends up working out now. I was using session_register and setcookie, but just ended up using
$_SESSION['x] = $foo;

I've used the exact same code, actually have it running on another website and it works fine, but I tried it here on a new server and I've been having to modify different things to make it work.
-FM-

eelixduppy

10:31 am on Apr 4, 2007 (gmt 0)



Maybe this is the reason why:

There is a defect in PHP 4.2.3 and earlier. If you register a new session variable by using session_register(), the entry in the global scope and the $_SESSION entry will not reference the same value until the next session_start(). I.e. a modification to the newly registered global variable will not be reflected by the $_SESSION entry. This has been corrected in PHP 4.3.0.

[php.net...]

:)