Well i wrote this a while ago and people told me to do this and that so i made a new one
session.php
[code=php]
<?php
class sessionHandler
{
protected static $_debug = 0;
protected static $_session = array();
protected static $getSessionByUid;
protected static $getSessionByIds;
protected static $updateSessionLastUse;
protected static $updateSession;
protected static $writeSession;
protected static $deleteSession;
protected static $deleteOldSession;
public static function init(&$db,$settings = array() )
{
// get session life time
self::$_session[‘lifetime’] = in_array(‘lifetime’ ,$settings) ? $settings[‘lifetime’] : get_cfg_var(“session.gc_maxlifetime”);
self::$_debug = $settings[‘debug’];
self::$getSessionByUid = $db->prepare(‘SELECT * FROM [sessions] WHERE `uid` = :UID’);
self::$getSessionByIds = $db->prepare(‘SELECT * FROM [sessions] WHERE `uid` = :UID AND `SID` = :SID’);
self::$updateSessionLastUse = $db->prepare(‘UPDATE [sessions] SET `time` = :time WHERE `sid` = :SID AND `UID` = :UID’);
self::$updateSession = $db->prepare(‘UPDATE [sessions] SET `value` = :value, `time`= :time WHERE `SID` = :SID AND `UID` = :UID’);
self::$writeSession = $db->prepare(‘INSERT INTO [sessions] (UID,SID,value,ip,time) VALUES (:UID,:SID,:value,:ip,:time)’);
self::$deleteSession = $db->prepare(‘DELETE FROM [sessions] WHERE UID = ? AND SID = ?’);
self::$deleteOldSession = $db->prepare(‘DELETE FROM [sessions] WHERE time < ?’);
self::garbage();
}
final private static function generate_uid()
{
while(empty(self::$_session[‘UID’]))
{
$UID = substr( md5( uniqid( rand(), true) ), 1, 18);
self::$getSessionByUid->execute(array(‘:UID’ => $UID));
$exists = self::$getSessionByUid->fetchAll();
if(empty($exists))
{
self::$_session[‘UID’] = $UID;
return $UID;
}
}
}
public function set_uid($UID)
{
if(empty($UID))
{
return self::generate_uid();
}
self::$getSessionByUid->execute(array(‘:UID’ => $UID));
$uid = self::$getSessionByUid->fetchAll();
if($uid[0][‘ip’] != $_SERVER[‘REMOTE_ADDR’])
{
return self::generate_uid();
}
return self::$_session[‘UID’] = $UID;
}
protected static function read($sid)
{
self::$getSessionByIds->execute(array(‘:SID’ => $sid, ‘:UID’ => self::$_session[‘UID’]));
$row = self::$getSessionByIds->fetchAll();
if(!empty($row))
{
self::$updateSessionLastUse->execute(array(‘:time’ => time() + self::$_session[‘lifetime’],’:SID’ => $sid, ‘:UID’ => self::$_session[‘UID’]));
}
return empty($row[0][‘value’]) ? 0 : unserialize($row[0][‘value’]);
}
protected static function write($sid, $value)
{
if($this->_debug)
{
echo ‘write@sessionHandler (‘.$sid.’:’.print_r($value).’)’;
}
self::$getSessionByIds->execute(array(self::$_session[‘UID’], $sid));
$exists = self::$getSessionByIds->fetchAll();
if(!empty($exists))
{
self::$updateSession->execute(array(‘:value’ => $value,’:time’ => time() +self::$_session[‘lifetime’],’:SID’ => $sid,’:UID’ => self::$_session[‘UID’]));
}
else
{
self::$writeSession->execute(array(‘:UID’ => self::$_session[‘UID’], ‘:SID’ => $sid, ‘:value’ => $value, ‘:ip’ => $_SERVER[‘REMOTE_ADDR’],’:time’ => time() + self::$_session[‘lifetime’]));
}
}
protected static function delete($sid)
{
self::$deleteSession->execute(array(self::$_session[‘UID’], $sid));
}
final private static function garbage()
{
self::$deleteOldSession->execute(array(time()));
}
}
final class session extends sessionHandler
{
public function __construct(&$db,$config)
{
parent::init($db,$config);
}
public function unRegister()
{
$names = func_get_args();
foreach($names as $name)
{
parent::delete($name);
}
}
public function __get($sid)
{
return parent::read($sid);
}
public function __set($sid,$value)
{
return parent::write($sid, serialize($value));
}
}
?>
how do use it
[code=php]
<?
function __autoload($class)
{
require_once(‘./cls’.DIRECTORY_SEPARATOR.strtolower($class).’.php’);
}
// connection to PDO with extended SQL class.
$db = new SQL($GLOBALS[‘config’][‘db’][‘host’],
$GLOBALS[‘config’][‘db’][‘user’],
$GLOBALS[‘config’][‘db’][‘pswd’],
$GLOBALS[‘config’][‘db’][‘db’],
$GLOBALS[‘config’][‘db’][‘system_prefix’],
$GLOBALS[‘config’][‘debug’]
);
$session = new session($db,array(‘lifetime’ => 10 * 60,’debug’ => $GLOBALS[‘config’][‘debug’]));
$cookie = new cookie();
$cookie->session_uid = $session->set_uid($cookie->session_uid);
$session->test = array(‘foo’ => ‘bar’);
print_r($session->test);
?>
result
[code]
array(
‘foo’ => ‘bar’
)
Please leave comment on how to make it better.