p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

  Return to Index  

beginning_php thread: Session using localhost on XP and IIS

Message #1 by "Jon Shoreman" <jon.shoreman@b...> on Thu, 31 Oct 2002 19:01:56
I have php and MySQL running OK on my local PC using Windows XP and IIS.

However, I cannot get sessions to work properly.

When I upload my code to the web everything is OK, so it must be a local 
setting, but I cannot figure out what.

At the start of each page I use 


I then check for a session variable and if not set, set it, as follows:

if (session_is_registered("EPUserID")==false)

Now, if I refresh the page the code now knows that the session variable 
is registered, but the variable is blank.

Again, this is only on my local PC. Everything is OK on the web.

Any ideas?


Message #2 by "Nikolai Devereaux" <yomama@u...> on Thu, 31 Oct 2002 13:17:09 -0800
I'd stay away from session_register and session_is_registered.  These mark
global variables as session variables, and therefore have caused more than a
little confusion, especially when people import form data into global scope as

Still, though, I don't see in your code where you attempt to extract the value
of the session variable.  You're probably just doing this:

echo $EPUserID;

However, if register_globals is off, then the next time you view the page, your
variable will still be in the $_SESSION array, but will not have been copied to
global scope.

If I remember correctly, you need to register a variable with the session on
every page that uses that variable as a session var.  Your code should look
more like this:


if(! isset($EPUserID) || ($EPUserID == ''))
    $EPUserID = 'GUEST';

echo $EPUserID;


It's much easier (and more organized) to forget about registering session
variables and access the session array directly.

For example:


if( !isset($_SESSION['EPUserID']) ||
    (isset($_SESSION['EPUserID']) == ''))
    $_SESSION['EPUserID'] = 'GUEST';

echo $_SESSION['EPUserID'];


If you understand references in PHP, then you'll realize that
session_register() can be more or less rewritten like this:

function session_register($varname)
      $_SESSION[$varname] = $GLOBALS[$varname];

   $GLOBALS[$varname] = & $_SESSION[$varname];
   return true;

All you're doing is saying that your global variable is just an alias
(reference) for the real session var, found in the $_SESSION array.

Hope this helps,


  Return to Index