session variables not registering

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
proto
Forum Newbie
Posts: 6
Joined: Tue Jul 05, 2005 1:25 pm

session variables not registering

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
proto
Forum Newbie
Posts: 6
Joined: Tue Jul 05, 2005 1:25 pm

Post by proto »

whoa it worked i guess the more complicated code was buggy. thanks a lot. i really needed the answer quickly.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply