Setting session vars in a function - some weirdness!

From: Date: Mon, 10 Sep 2001 19:49:01 +0000
Subject: Setting session vars in a function - some weirdness!
Groups: php.general 
Request: Send a blank email to php-general+get-66813@lists.php.net to get a copy of this message
One of the annotations to the docs for session functions notes that if you want to set a session variable inside a function, you have to call session_start() BEFORE to use your global $varname statement. But there's more to it than that. After some very annoying trial and error, I disovered that this won't work: function setFlavor ($flavor) { // THIS WON'T WORK!!!! session_start(); global $HTTP_SESSION_VARS; session_register("sess_flavor"); $HTTP_SESSION_VARS["sess_flavor"] = $flavor; } But this will: function setFlavor ($flavor) { // THIS WILL!!! session_start(); global $sess_flavor; session_register("sess_flavor"); $sess_flavor = $flavor; } In other words, using $HTTP_SESSION_VARS won't work for writing a session variable within a function. You have to use the bare varname. What's odd is using $HTTP_SESSION_VARS to read a variable works fine. Since I like to always use $HTTP_SESSION_VARS for safety, rather than the bare varname, I'm not too thrilled with all this. Can anyone explain why it's so? Thanks, Chris

« previous php.general (#66813) next »