My code below for reference (any suggestions/improvements welcome)...
Code: Select all
<?php
/*
* Session Control
*
* @desc Control of SESSION data and stores in the database instead of in files in the temp directory (/tmp)
* @example new sessionCtrl; // create instance of the object at the top of every page and the object will take care of the rest
* @version 0.0.4
* @author Eddie Jaoude
*/
class sessionCtrl
{
/*
* @desc database connection
* @access protected
* @var resource
*/
static protected $_rConnection;
/*
* @desc default lifetime expiry - this is overwritten by php.ini file
* @access protected
* @var integer
*/
public $lifetime = 7200;
/*
* @desc default method - run everytime object is initiated. Setup configuration
* @access public
* @param void
* @return void
*/
public function __construct()
{
# get lifetime
$this->lifetime = get_cfg_var('session.gc_maxlifetime');
# remove any session auto start
session_write_close();
# set functions for SESSIONs to use
# modified to use object reference making it more robust
session_set_save_handler( array(&$this, 'start'),
array(&$this, 'end'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc'));
# start the SESSION as normal
session_start();
}
/*
* @desc start - run on session_start() in the __construct method. Initiates the session creation in the database
* @access public
* @param $save_path string
* @param $name string
* @return boolean
*/
public function start($save_path, $name)
{
self::$_rConnection = mysql_connect('localhost','******','*******');
$database = mysql_select_db('session', self::$_rConnection);
return $database;
}
/*
* @desc end - run on session_close(). Closes the database connection
* @access public
* @param $save_path string
* @param $name string
* @return boolean
*/
public function end()
{
$close = mysql_close(self::$_rConnection);
return $close;
}
/*
* @desc read - reads the session from the database based on the unique id
* @access public
* @param $id string
* @return string
*/
public function read($id)
{
$q = 'SELECT *
FROM session
WHERE id = "'.$id.'"';
$sql = mysql_query($q, self::$_rConnection);
if (mysql_num_rows($sql))
{
$result = mysql_fetch_assoc($sql);
return $result['data'];
}
}
/*
* @desc write - writes the session to the database. If session->id does not already exist INSERT otherwise UPDATE (REPLACE)
* @access public
* @param $id string
* @param $data string
* @return boolean
*/
public function write($id, $data)
{
$q = 'REPLACE INTO session
SET
id = "'.$id.'",
data = "'.mysql_real_escape_string($data).'"';
$sql = mysql_query($q, self::$_rConnection);
return $sql;
}
/*
* @desc destroy - deletes the session from the database based on the session->id
* @access public
* @param $id string
* @return boolean
*/
public function destroy($id)
{
$q = 'DELETE
FROM session
WHERE id = "'.$id.'"';
$sql = mysql_query($q, self::$_rConnection);
return $sql;
}
/*
* @desc garbage collection - runs randomly, probability is in the php.ini file
* @access public
* @param $id string
* @return boolean
*/
public function gc()
{
$q = 'DELETE FROM session
WHERE modified < DATE_SUB(NOW(), INTERVAL '.$this->lifetime.' SECOND)';
$sql = mysql_query($q, self::$_rConnection);
return $sql;
}
}
?>