RE: [PHP] Session variable scope - surprise!
I just read further on the manual page for session_register. It also contains the following: Caution This registers a global variable. If you want to register a session variable inside a function, you need to make sure to make it global using global() or use the session arrays as noted below. Richy == Richard Black Systems Programmer, DataVisibility Ltd - http://www.datavisibility.com Tel: 0141 435 3504 Email: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Session variable scope - surprise!
>From the manual page for session_register on PHP.net: "For each name, session_register() registers the global variable with that name in the current session. " I tried your script below. The following was created in my session file: !test| Which I'm guessing means a variable which has not been instantiated. If it had held a value, it would look something like test|i:13; So to answer your questions... Session_register on an undefined global variable produces something like the above in the session file. Accessing the value later in the script has to depend on it being global, since to be registered in the session it has to be a global variable in the first place. HTH, Richy == Richard Black Systems Programmer, DataVisibility Ltd - http://www.datavisibility.com Tel: 0141 435 3504 Email: [EMAIL PROTECTED] -Original Message- From: Richard Fox [mailto:[EMAIL PROTECTED]] Sent: 04 March 2002 16:03 To: PHP Subject: RE: [PHP] Session variable scope - surprise! Then, when session_register(""); is called (even on a variable which is going out of scope) what actually happens? Where is the data actually stored? I know it isn't in the file yet (I have session.save_handler = files) because nothing is written to the file until the script exits. So the memory for should either be in free store or on the stack. And, the second question: how can this memory, and the data it contains, be accessed later in the script (after the session_register) without having to depend on the original variable being global or not? Indeed, it appears that unless the registered variable is global, session_register will not work. (Reference my test script appended below, which can be loaded multiple times with no effect) This is undocumented! Note: I am not complaining about documentation, I am just trying to understand what exactly is happening when you register a session variable. Richard "; } session_start(); if (!session_is_registered("test")) reg(); echo "HTTP_SESSION_VARS[test] = <".$HTTP_SESSION_VARS["test"].">"; ?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Session variable scope - surprise!
Then, when session_register(""); is called (even on a variable which is going out of scope) what actually happens? Where is the data actually stored? I know it isn't in the file yet (I have session.save_handler = files) because nothing is written to the file until the script exits. So the memory for should either be in free store or on the stack. And, the second question: how can this memory, and the data it contains, be accessed later in the script (after the session_register) without having to depend on the original variable being global or not? Indeed, it appears that unless the registered variable is global, session_register will not work. (Reference my test script appended below, which can be loaded multiple times with no effect) This is undocumented! Note: I am not complaining about documentation, I am just trying to understand what exactly is happening when you register a session variable. Richard "; } session_start(); if (!session_is_registered("test")) reg(); echo "HTTP_SESSION_VARS[test] = <".$HTTP_SESSION_VARS["test"].">"; ?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Session variable scope - surprise!
$HTTP_SESSION_VARS only gets populated when you load another page. Or refresh the current one. To use your example again... "; } global $HTTP_SESSION_VARS; session_start(); $test = 12; reg(); if (session_is_registered("test")) { $tt = $HTTP_SESSION_VARS["test"]; echo "session variable test=<$tt>"; } else echo "You really don't know what you're doing, do you?"; ?> Run this script, and it will give the output in reg(), test=<13> session variable test=<> But... Refresh the page, and you should see: in reg(), test=<13> session variable test=<12> Because $HTTP_SESSION_VARS gets populated. Its something to do with HTTP headers I think - can't remember exactly why, but I know what happens. HTH, == Richard Black Systems Programmer, DataVisibility Ltd - http://www.datavisibility.com Tel: 0141 435 3504 Email: [EMAIL PROTECTED] -Original Message- From: Richard Fox [mailto:[EMAIL PROTECTED]] Sent: 04 March 2002 15:26 To: [EMAIL PROTECTED] Subject: RE: [PHP] Session variable scope - surprise! OK, but then how do you explain: "; } global $HTTP_SESSION_VARS; session_start(); reg(); if (session_is_registered("test")) { $tt = $HTTP_SESSION_VARS["test"]; echo "session variable test=<$tt>"; } else echo "You really don't know what you're doing, do you?"; ?> This still gives me the output: in reg(), test=<13> session variable test=<> Aaargh! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Session variable scope - surprise!
OK, but then how do you explain: "; } global $HTTP_SESSION_VARS; session_start(); reg(); if (session_is_registered("test")) { $tt = $HTTP_SESSION_VARS["test"]; echo "session variable test=<$tt>"; } else echo "You really don't know what you're doing, do you?"; ?> This still gives me the output: in reg(), test=<13> session variable test=<> Aaargh! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Session variable scope - surprise!
I think session_register registers the variable and its value as a global. In this case, $test's global value is nothing. nada. Because it doesn't exist globally. In the reg() function, what is being output is a LOCAL variable called $test. Not a global value. If you put the line global $test; into the reg() function so that it reads: function reg() { global $test; $test = 13; session_register("test"); echo "in reg(), test=<$test>"; } Then it should work as expected. Sure it *does* say in the docs somewhere that register_globals() is only applicable to global variables. HTH, Richy == Richard Black Systems Programmer, DataVisibility Ltd - http://www.datavisibility.com Tel: 0141 435 3504 Email: [EMAIL PROTECTED] -Original Message- From: Richard Fox [mailto:[EMAIL PROTECTED]] Sent: 04 March 2002 14:57 To: [EMAIL PROTECTED] Subject: [PHP] Session variable scope - surprise! This is a behavior I have not found documented, in fact the books I have read indicated that this would not be the case... given the script (with session.save_handler = files): "; } session_start(); reg(); echo "test=<$test>"; ?> The output is: in reg(), test=<13> test=<> I was quite surprised, but at least now I know why my code isn't working. I assume that $test is going out of scope after the function reg() exits. BUT, isn't registering a variable as a session variable supposed to give it "superglobal" status? I thought so. Can someone give me the 'official' explanation of this behavior? Many thanks, Richard -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php