When are sessions written?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Mark Rose
Forum Newbie
Posts: 1
Joined: Thu Mar 19, 2009 4:35 pm

When are sessions written?

Post by Mark Rose »

Greetings,

I'm looking at doing some custom sessions handling. I'm design a scalable site that could potentially handle thousands of hits a second. I'll be using multiple webservers and a shared session storage mechanism. I'm thinking about using Memcache to cache sessions in between the web servers and Mysql. I don't want to use Memcache only as then all the users whose cookies are on a given Memcache server would be logged out if that Memcache server went down.

So this leads me to a question I haven't been able to find the answer to on the web: when are sessions saved? Is the write handler called at the end of every script execution? Or is it called only when the session data changed? The answer to this question will make a big difference in my approach.

Thanks!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: When are sessions written?

Post by Christopher »

Sessions are saved after execution completes and the output stream is closed. I assume that the save handler is always called if the session has been started.

Maybe there is something about this in the comments, but it would be easy to test:

http://us.php.net/manual/en/function.se ... andler.php
(#10850)
Post Reply