Page 1 of 1

When are sessions written?

Posted: Thu Mar 19, 2009 4:42 pm
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!

Re: When are sessions written?

Posted: Thu Mar 19, 2009 4:51 pm
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