Falling back to PHP's session handler

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi all,

I've created a custom session handler that saves session data to a
MySQL database. If the database server is down, I'd like to fallback to
PHP's default session handler (files) until the server comes back up.

After calling session_set_save_handler(), is there a way to tell PHP to
ignore this setting and revert to temp files for session storage? If I
try an ini_set on session.save_handler upon detecting that the DB
server is down, it doesn't work because the session is already active.


Re: Falling back to PHP's session handler

usenet@shat.net wrote:
Quoted text here. Click to load it

Try shutting down the session by calling session_write_close() before
you call ini_set(). The update handler on session.save_handler checks
the session status. Resetting the status to inactive should allow you
to have the handler back to "files."

I wonder how well such a fallback mechanism would work. Active sessions
at the moment when the database goes down would be lost. The same thing
would happen when the database comes back online as the session files
are now ignored.

Re: Falling back to PHP's session handler

This did the trick, though I discovered along the way that PHP is
extremely picky about when the call to session_write_close() is made.

When the database goes offline or comes back, the active sessions will
be lost, but I'm trying to plan for the "3AM contingency." Suppose the
DB goes down for an hour, I'm not too worried about having a handful of
sessions drop at the onset, I'm more interested in making sure that
everyone else's sessions work during the downtime.

Thanks again :)

Re: Falling back to PHP's session handler

usenet@shat.net wrote:

Quoted text here. Click to load it
You're trying to solve the wrong problems in the wrong way.

Unless you've got a very unusual application, the biggest problem you are
going to have is detecting the state of the DBMS. You don't want to be
waiting for every database operation to timeout. This then solves your
problem but shifts the emphasis onto a reliable method for detecting an

Since the interval between reading and writing a session *should be* a lot
shorter than the interval between writing and reading a session, there
there will be very little chance of rescuing session data during the
failure. So don't waste your time trying to change the save handler after
you've set it up. Again this makes your life simpler, although its possibly
not the news you wanted to hear.

This just leaves a simple if wrapper around the session handler:

if ((file_exists($database_working_semaphore)) || (rand(0,100)>98)) {
        if (test_db_connection()) {
                file_put_contents($database_working_semaphore, time());
        } else {
if (file_exists($database_working_semaphore)) {
        // use database session handler
} else {
        // use file handler

Now if you are wanting a fault-tolerant solution instead of a failover
solution....that's a different story. But you'll have to raise your game a
lot. (ouch ! mixed metaphor).


Site Timeline