Location: PHPKode > scripts > MySQL Done Right > mysql-done-right/sessions.inc
<?php
/*
    Purpose       : Provides php session storage in mysql.  
                    Based on my database abstraction class mysqli_ext.
    Requires      : mysqli_ext -> dbconnect.inc
    Version       : 0.24
    File          : sessions.inc
    Author        : Daren Schwenke
    Date released : 2009-10-19 
    License       : http://www.gnu.org/licenses/lgpl.txt
    Notes         : Require this after dbconnect, and before session_start():
                    require('dbconnect.inc');
                    require('sessions.inc');
                    session_start();
  
                    Timestamps are derived local to database to allow multiple 
                    webservers to share session information and not rely on 
                    local time of the webservers being synced.
       
                    To use this properly, you MUST call session_write_close() 
                    after your changes to session variables, and before your 
                    code exits.  Otherwise, the database handle will disappear 
                    before the final write, and your data will not be saved.

Format of needed database table assuming SESSIONS_TABLE name of 'sessions':

  CREATE TABLE IF NOT EXISTS `sessions` (
    `session_id` varchar(32) NOT NULL,
    `session_time` int(11) NOT NULL,
    `session_ip` varchar(16) NOT NULL,
    `session_user_agent` varchar(255) NOT NULL,
    `session_data` mediumtext NOT NULL,
    PRIMARY KEY  (`session_id`),
    KEY `session_time` (`session_time`)
  ) ENGINE=MyISAM DEFAULT CHARSET=utf8;


*/
 
# Table name for sessions within database.
define('SESSIONS_TABLE','sessions');
# I like to store extra information about user_ip and user_agent in sessions.
# Slows things down a bit, but has useful stuff when needed.
# Setting following to true will enable this extended information.
define('SESSIONS_DEBUG',false);
#define('SESSIONS_DEBUG',true);
# Expire time for sessions.
ini_set('session.gc_maxlifetime', 3600);
# Other stuff.
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 100);

# Use our handler instead of php builtin.
ini_set('session.save_handler','user');

function mysqli_ext_session_open($path, $name) {
  return true;
}
function mysqli_ext_session_close() {
  return true;
}
function mysqli_ext_session_read($id) {
  global $db;
  $session = $db->psingle('SELECT session_data FROM ' . SESSIONS_TABLE . ' WHERE session_id = ? LIMIT 1','s',$id);
  if (! $session) { return ''; }
  return $session->session_data;
}
function mysqli_ext_session_write($id, $data) {
  global $db;
  if ( SESSIONS_DEBUG ) { 
    return (bool)$db->pexecute('REPLACE INTO ' . SESSIONS_TABLE . ' (session_id, session_time, session_ip, session_user_agent, session_data) VALUES (?,UNIX_TIMESTAMP(),?,?,?)','ssss',$id,$_SERVER['REMOTE_ADDR'],$_SERVER['HTTP_USER_AGENT'],$data);
  } else {
    return (bool)$db->pexecute('REPLACE INTO ' . SESSIONS_TABLE . ' (session_id, session_time, session_data) VALUES (?,UNIX_TIMESTAMP(),?)','ss',$id,$data);
  }
}
function mysqli_ext_session_destroy($id) {
  global $db;
  return (bool)$db->pexecute('DELETE FROM ' . SESSIONS_TABLE . ' WHERE session_id = ? LIMIT 1','s',$id);
}
function mysqli_ext_session_gc($life) {
  global $db;
  return (bool)$db->pexecute('DELETE FROM ' . SESSIONS_TABLE . ' WHERE session_time < ( UNIX_TIMESTAMP() - ? )','i',$life);
}
session_set_save_handler ('mysqli_ext_session_open', 'mysqli_ext_session_close', 'mysqli_ext_session_read', 'mysqli_ext_session_write', 'mysqli_ext_session_destroy', 'mysqli_ext_session_gc');
Return current item: MySQL Done Right