RE: [PHP] Creating Session Variables

From: Date: Fri, 04 Apr 2003 03:55:56 +0000
Subject: RE: [PHP] Creating Session Variables
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-142244@lists.php.net to get a copy of this message
> > No, $varname isn't created. You don't need it. You have a variable > called > > $_SESSION['varname'], just use that where ever you need it (even within > > functions). If register_globals is on, you should be using the > > session_register() method, anyhow, not the one above. > > I have register_globals off as that is (as I udnerstand it) a more > secure approach. ANd it will make hte code messy to use > $_SESSION['varname'] everywhere. I think I'll just copy it to a global > and use it that way. You think it'll make your code "messy" but to anyone else that has to eventually read your code, it'll be a lot clearer to them. Also, when you come back later to edit this code, you'll know exactly where $_SESSION['varname'] came from, instead of having to wade through your code and find the part where you say $varname = $_SESSION['varname']. I don't understand why people are so afraid to just use the $_GET, $_POST, etc arrays in their code and have to assign them to temp variables. You're just wasting time and memory. > Which leads to another question ... I thought these two constructs were > equivalent: > > $var = "foo"; > session_register('var'); > ..... > $var = "bar"; > > $var = "foo"; > $_SESSION['var'] = $var; > ..... > $var = "bar"; > > I would have thought that in both cases the session data saved at the > end of the script will have $var set to "bar", Well, $var will be "bar" at the end of each script, but only the first example (with register globals on) will actually change the session value. With register globals off, $_SESSION['var'] and $var are two totally different variables. Just get in the habit of using $_SESSION['varname'], you'll be better off for it. ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/

« previous php.general (#142244) next »