RE: [PHP] Session variables and register_global
| From: | Johnson, Kirk | Date: | Wed, 04 Apr 2001 15:28:14 +0000 |
| Subject: | RE: [PHP] Session variables and register_global | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-47074@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Niklas Neumann [mailto:usenet21@n-neumann.de]
> Sent: Wednesday, April 04, 2001 3:20 AM
> To: php-general@lists.php.net
> Subject: [PHP] Session variables and register_global
>
> session_register("test");
> $GLOBALS['HTTP_SESSION_VARS']['test'] = 'Hello World!';
>
> does not work properly (session variable test is not
> initialized with the
> value 'Hello World!') if register_globals is turned on, but
> when it is
> turned off?
$test and $GLOBALS['HTTP_SESSION_VARS']['test'] are *not* the same variable
*while on the current page*:
$test = "Goodbye cruel world!";
session_register("test");
$GLOBALS['HTTP_SESSION_VARS']['test'] = 'Hello World!';
echo $test; // prints Goodbye cruel world!
echo $GLOBALS['HTTP_SESSION_VARS']['test']; // prints Hello World!
What happens on the *next page* depends on whether register_globals is on or
off. If it is on, then:
echo $test; // prints Goodbye cruel world!
echo $GLOBALS['HTTP_SESSION_VARS']['test']; // also prints Goodbye cruel
world!
The value in the GLOBAL variable $test gets written to
$GLOBALS['HTTP_SESSION_VARS']['test'] when PHP stores the session variables
between pages.
If register_globals is off, then:
echo $test; // prints nothing
echo $GLOBALS['HTTP_SESSION_VARS']['test']; // prints Hello World!
This is because there are no session variables outside of $HTTP_SESSION_VARS
when register_globals is off. Also, all references to POST/GET variables
must be made through the corresponding $HTTP_***_VARS arrays.
This is a complicated aspect of PHP, and the manual is not real clear on how
this all works. I strongly urge you to write up some short test code and see
for yourself what is going on with register_globals.
Kirk