All in the <head>

– Ponderings & code by Drew McLellan –

– Live from The Internets since 2003 –

About

Sessions, hah! What are they good for?

3 December 2003

We’ve been having some trouble at work with PHP session variables. It’s an evil Windows installation (don’t blame me, blame Pitman), I’ve pretty much concluded that it’s simply the implementation being flaky rather than a specific bug in the code. We’re getting a lot of now-it’s-working, now-it’s-not (NIWNIN?). Basically I suspect we might end up abandoning using PHP session management and rolling our own. What’s a girl to do?

The question of the day is – how do you manage your sessions? Are they any nice classes out there worth using? Should we stick with plain old cookies or use something more URLified? I see that with the PHP session handling functions you can set up your own save handler to use whatever mechanism you like. Possibly we might have more success setting a handler that uses our MySQL database. However, I’m not sure where within the PHP session handling the problem is occurring, so that may well not be an answer.

Any insights as to how you handle session management in PHP are seriously welcomed.

- Drew McLellan

Comments

  1. § George: I’v never had any problems with session’s except for Windows, but changing the folder sessions are stored in (/tmp doesn’t exist in windows) cured it. I did write a custom, session handling class for a project I am working on, stores the info in a MySQL DB. Wasn’t difficullt, and actually performs better than the ”traditional way” of storing session info.
  2. § Dysfunksional.Monkey: I’ve been working with PHP as my primary language for more than 5 years now, on both wimp (Win/IIS/MySQL/PHP) and lamp (Linux/Apache/MySQL/PHP) boxes. I’ve never had any problems with sessions.

    On windows boxes, you have to manually set-up the folder sessions are stored in. I use something like ”D:\session.temp\” or whatever. For security (and possibly leagal) reasons, its a good idea to turn off cookies in the php.ini file, and run sessions from the url. Also, change the variable PHPSESSID to something else.

    Make sure you generate your own session id using md5, test it regularly, and be sure to set a reasonable time limit for your sessions. How many times have you sat at a website for longer than 30 mins?

    If you’re looking to keep your app consistant with OOP, you might want to create a class with getter and setter functions, along with create/test/destroy functions.

    Also, subscribe to PHP Architect.
  3. § Massimo Foti: First of all check to be sure the path to the directory where sessions are stored is correct.

    Apart from this, unfortunately the way sessions are implemented in PHP, writing/reading files, is quite primitive... You should be able to find libraries to store sessions inside a database.

    Massimo
  4. § b.: Roll your own using HEAP tables. Why would anyone even consider doing it another way?
  5. § Dysfunksional.Monkey: Database load, perhaps?

Textile Help

Photographs

Work With Me

edgeofmyseat.com logo

At edgeofmyseat.com we build custom content management systems, ecommerce solutions and develop web apps.

Recent Links

Affiliation

  • Web Standards Project
  • Britpack
  • 24 ways

About Drew McLellan

Photo of Drew McLellan

Drew McLellan has been hacking on the web since around 1996 following an unfortunate incident with a margarine tub. Since then he’s spread himself between both front- and back-end development projects, and now is Director and Senior Web Developer at edgeofmyseat.com in Maidenhead, UK (GEO: 51.5217, -0.7177). Prior to this, Drew was a Web Developer for Yahoo!, and before that primarily worked as a technical lead within design and branding agencies for clients such as Nissan, Goodyear Dunlop, Siemens/Bosch, Cadburys, ICI Dulux and Virgin.net. Somewhere along the way, Drew managed to get himself embroiled with Dreamweaver and was made an early Macromedia Evangelist for that product. This lead to book deals, public appearances, fame, glory, and his eventual downfall.

Picking himself up again, Drew is now a strong advocate for best practises, and stood as Group Lead for The Web Standards Project 2006-08. He has had articles published by A List Apart, Adobe, and O’Reilly Media’s XML.com, mostly due to mistaken identity. Drew is a proponent of the lower-case semantic web, and is currently expending energies in the direction of the microformats movement, with particular interests in making parsers an off-the-shelf commodity and developing simple UI conventions. He writes here at all in the head and, with a little help from his friends, at 24 ways.