Falling back to PHP's session handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • usenet@shat.net

    Falling back to PHP's session handler

    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_sav e_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_ha ndler upon detecting that the DB
    server is down, it doesn't work because the session is already active.

    Thanks

  • Chung Leong

    #2
    Re: Falling back to PHP's session handler

    usenet@shat.net wrote:[color=blue]
    > 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_sav e_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_ha ndler upon detecting that the DB
    > server is down, it doesn't work because the session is already active.
    >
    > Thanks[/color]

    Try shutting down the session by calling session_write_c lose() before
    you call ini_set(). The update handler on session.save_ha ndler 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.

    Comment

    • usenet@shat.net

      #3
      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_c lose() 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 :)

      Comment

      • Colin McKinnon

        #4
        Re: Falling back to PHP's session handler

        usenet@shat.net wrote:
        [color=blue]
        >
        > 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_sav e_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_ha ndler upon detecting that the DB
        > server is down, it doesn't work because the session is already active.
        >[/color]
        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
        outage.

        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_workin g_semaphore)) || (rand(0,100)>98 )) {
        if (test_db_connec tion()) {
        file_put_conten ts($database_wo rking_semaphore , time());
        } else {
        unlink($databas e_working_semap hore);
        }
        }
        if (file_exists($d atabase_working _semaphore)) {
        // use database session handler
        } else {
        // use file handler
        }

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

        C.

        Comment

        Working...