The $_SESSION PHP Superglobal

Posted in: Development, Tasks Pro™

I’m using the $_SESSION superglobal in my multi-user tasks development instead of the older $HTTP_SESSION_VARS superglobal. To keep backward compatability, I was creating a $_SESSION variable if one didn’t already exist and just setting it to $HTTP_SESSION_VARS. This way, I could just always look for the $_SESSION variable and still be compatible with older versions of PHP.

Then I ran into some trouble.

If you are running a version of PHP that doesn’t have the $_SESSION variable, I need to declare the $_SESSION variable I created as a global variable within a function if I want to reference the data in the $_SESSION. If I declare the $_SESSION variable as global in a version of PHP that already has the $_SESSION variable, I break it.

One solution would be to write my own code for putting and retrieving data into sessions. I could wrap everything and talk to either the $_SESSION or $HTTP_SESSION_VARS as needed. I’d also have to do this for the $_REQUEST variable and others. This would be a lot of extra code. Actually, I bet I’ve already broken this somewhere. Turning on globals is not an option.

I think I’m just going to require PHP 4.2 or later and call it good. Solutions, comments and constructive arguments for backward compatability are welcome.

UPDATE: Hmm, actually I could pretty easily include a ‘compatible_vars.inc.php’ page that did the checking and :scare: global-ing :/scare: within the functions that need it. I’ll try that if no better solutions present themselves.

Popularity: 3% [?]

Posted October 28th, 2003 @ 12:41 AM

4 Replies

  1. Kenneth Power adds this Comment:

    In one of my projects, what I did was create a light messenger object that is passed to functions/methods/objects. In the constructor for this object, all variables in $_GET, $_POST, and $_SESSION are turned into object variables. Not the most elegant solution, with problems of its own, but it alowed me to overcome a similar problem.

    October 28th, 2003 at 6:30 am

  2. alexking.org: Blog adds this Pingback:

    [...] uperglobals Hack I’ve added my superglobals hack to tasks. The code looks like this: This file (superglobals.inc.p [...]

    October 30th, 2003 at 12:11 am

  3. Melissa adds this Comment:

    I need your help in fixen the code i am using for my internet sever.My login page is written using a lot of session butits not bringng the sessions from one page to the next and I cant figure out why could you help me with this promblem if so could you email me back

    April 27th, 2004 at 3:36 am

  4. Anand Singh adds this Comment:

    using the $_SESSION but i can’t understand why this used?

    June 22nd, 2009 at 5:58 am

Add a Comment

Please note: Use of a non-personal web site or blog URL in the field below and/or comments that are off-topic, personal attacks, or support requests will likely be removed at my discretion. If you need help with WordPress or a WordPress plugin, please contact the WordPress HelpCenter.

Note: This post is over 5 years old. You may want to check later in this blog to see if there is new information relevant to your comment.

» «

About This Site

This is the personal web site of Alex King, a web developer in Denver, Colorado USA. More...


Crowd Favorite

Crowd Favorite is my software and web development business.

We build web applications, design and develop custom WordPress themes and plugins, and build custom sites using WordPress as a CMS.


I also have a tumblog that aggregates my online content from other services (Twitter, Flickr, del.icio.us. etc.).

America - america09.com