Page 1 of 1

session variables not registering

Posted: Tue Jul 05, 2005 4:05 pm
by proto
i first encountered the problem of having session variables not pass between documents while trying to build an authentication program for a site im building. nothing i tried could get it to work. i then attempted this script:

Code: Select all

<?
session_start();
session_register("sessvar");
$sessvar="qwerty";
echo($sessvar);
?>
<a href="next.php">next</a>
next.php:

Code: Select all

<?
session_start();
echo($HTTP_SESSION_VARS["sessvar"]);
?>
i get no output whatsoever. im thinking its a php.ini problem. its driving me crazy.
i also tried using $sessvar instead of $HTTP_SESSION_VARS["sessvar"] after i saw that register_globals was on.

Posted: Tue Jul 05, 2005 4:21 pm
by pickle
You're using a valid, but deprecated way of accessing session variables. It's now this easy:

Code: Select all

<?PHP
session_start();
$_SESSION['sessvar'] = 'Hello word';
?>

<?PHP
session_start();
echo $_SESSION['sessvar'];
?>
try calling print_r($_SESSION) and count($_SESSION) to get an idea what's there.

There are a bunch of session settings in php.ini, but I don't know much about any of them.

Posted: Tue Jul 05, 2005 5:34 pm
by proto
whoa it worked i guess the more complicated code was buggy. thanks a lot. i really needed the answer quickly.

Posted: Tue Jul 05, 2005 6:09 pm
by pickle
FYI: in your original code, you were registering the session variable, but never assigning a value to it. If you were to print_r($_SESSION) in your initial code, you would see an array with "sessvar" in it, but no value. Even after registering "sessvar" as a session variable, you still need to assign it a value - referencing it all the while as a session variable, not a regular variable.

Kind of a moot point now that you've got it working, but at least now you know.