RE: [PHP] Session problem
| From: | John W. Holmes | Date: | Sun, 18 May 2003 18:19:09 +0000 |
| Subject: | RE: [PHP] Session problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-148108@lists.php.net to get a copy of this message | ||
> i have two php scripts the first one says:
> test.php
> <?php
> session_start();
>
> if(!isset($_SESSION['testsession']))
> $_SESSION['testsession']=0;
>
> //empty session array
> $testsession=array();
>
> $testsession['testthis']='hello';
>
> echo(isset($testsession['testthis'])); //this prints
> true
It is true because you just defined the variable directly before this.
It has no relation to the session at all, though.
> header("Location: test2.php");
> ?>
>
> test2.php
> <?php
> session_start();
>
> echo(isset($testsession['testthis'])); //this prints
> false
It should, there is no $testsession var in this script. You need to use
the $_SESSION array.
> session_unregister('testsession');
Don't do this, either. Use unset($_SESSION['testsession']);
Either use the $_SESSION array for everything, or use the session_*
functions (with register_globals on). Just because you have
$_SESSION['var'] = 1; it does not mean session_is_registered('var') will
return true. You can't mix the two.
So you want to set a session var:
$_SESSION['testsession'] = array();
$_SESSION['testsession']['testthis'] = 'hello';
And then to check later if it's set, use
If(isset($_SESSION['testsession']['testthis']))
---John W. Holmes...
PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/