All in the <head>

– Ponderings & code by Drew McLellan –

– Live from The Internets since 2003 –

About

PHP Sessions Update

5 December 2003

If you read Sessions, hah! What are they good for you would have noticed that I was having intermittent problems with session variables in PHP (on Windows – yuck). Thanks for all the helpful comments.

I finally tracked down the problem, and discovered a solution to boot. It would seem that if you set a session variable and then later in your script redirect to a different page, the session variable will sometimes get lost. I can see the logic of this in as far as setting a cookie and then modifying the headers – it’s a reasonable side affect that the cookie might get lost – but why is it intermittent? I thought computers were supposed to be deterministic.

Anyway, if you’re going to set a session variable and then redirect, best to close the session object first:

$_SESSION['foo'] = $bar;
session_write_close();

header("Location: http://example.com/");
exit;

- Drew McLellan

Tags

Comments

  1. § Danilo: Computers are very deterministic, they are determined to make it quite difficult to detemine what the heck went wrong!
  2. § Dysfunksional.Monkey: Quite simply, if you set a session variable and send a redirection header, the session handling code might not be able to update the cookie before the redirect finishes.
  3. § George Buford: Thanks for this. Saved me days of headaches.

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.