PHP Front
The PHP website created by its users!
Home » PHP » db session handler


db session handler


Authorandreas beder
Descriptionits a database session handler based on mdb2.
RatingThis script has been rated 4/5 (2 votes)

PHP Code


?php
class Session {
    // session-lifetime
    public $lifeTime;
    function __construct ($db) {
        // get session-lifetime
        $this->lifeTime = get_cfg_var("session.gc_maxlifetime");
           // open database-connection
        $this->mdb2 =& MDB2::factory($db);
        if (PEAR::isError($this->mdb2)) {
            $php_errormsg .= $this->mdb2->getMessage();
            $php_errormsg .= $this->mdb2->getDebugInfo();
        }
        session_set_save_handler(array(&$this, 'open'),
                                array(&$this, 'close'),
                                array(&$this, 'read'),
                                array(&$this, 'write'),
                                array(&$this, 'destroy'),
                                array(&$this, 'gc'));
        register_shutdown_function('session_write_close');
        session_start();
           return true;
    }
    function open($savePath, $sessName) {
        // get session-lifetime
        $this->lifeTime = get_cfg_var("session.gc_maxlifetime");
        return true;
    }
    function close() {
        $this->gc(ini_get('session.gc_maxlifetime'));
        // close database-connection
        return $this->mdb2->disconnect();
    }
    function read($sessID) {
        global $php_errormsg;
        // fetch session-data
        $query = "
            SELECT session_data FROM sessions
            WHERE session = '$sessID'
            AND session_expires >
        ".time();
        $result = $this->mdb2->query($query);
        // return data or an empty string at failure
        if (MDB2::isError($result)) {
            $php_errormsg .= $result->getMessage();
            $php_errormsg .= $result->getDebugInfo ();
            return false;
        }
        list($value)=@$result->fetchrow();
        return $value;
    }
    function write($sessID,$sessData) {
        global $php_errormsg;
        // new session-expire-time
        $newExp = time() + $this->lifeTime;
        // is a session with this id in the database?
        $query = "
            SELECT * FROM sessions
            WHERE session = '$sessID'
        ";
        $result = $this->mdb2->query($query);
        // if yes,
          if($result->numRows()) {
        // ...update session-data
            $query = "
                UPDATE sessions
                SET session_expires = '$newExp',
                 session_data = '$sessData'
                WHERE session = '$sessID'
            ";
          }
        // if no session-data was found,
          else {
            // create a new row
            $query = "
                INSERT INTO sessions (
                     session,
                      session_expires,
                      session_data)
                VALUES(
                     '$sessID',
                      '$newExp',
                      '$sessData')
            ";
          }
        $result = $this->mdb2->exec($query);
        // if something happened, return true
        if (MDB2::isError($result)) {
            $php_errormsg .= $result->getMessage();
            $php_errormsg .= $result->getDebugInfo ();
            return false;
        } else {
            // ...else return true
            return true;
        }
    }
    function destroy($sessID) {
        global $php_errormsg;
        // delete session-data
        $query = "
            DELETE FROM sessions
            WHERE session = '$sessID'
        ";
        $opt_db="OPTIMIZE TABLE sessions";  
        $result = $this->mdb2->exec($query);
        $resultopt = $this->mdb2->exec($opt_db);
        // if session was not deleted, return false,
         if (MDB2::isError($result)) {
            $php_errormsg .= $result->getMessage();
            $php_errormsg .= $result->getDebugInfo ();
            return false;
         } else {
            // ...else return true
            return true;
        }
    }
    function gc($sessMaxLifeTime) {
        global $php_errormsg;
        // delete old sessions
        $query = "
            DELETE FROM sessions
            WHERE session_expires <
        ".time();
        $result = $this->mdb2->exec($query);
        // return affected rows
        if (MDB2::isError($result)) {
            $php_errormsg .= $result->getMessage();
            $php_errormsg .= $result->getDebugInfo ();
        }
        return $result;
    }
}
?>



Rate it: Print Print

Comments


No comments posted yet.

Name
Website
Your comment