include_once 'classDataBase.php';
if(!class_exists('mysql_session_handler')) {
class mysql_session_handler {
static $life_time;
static $mysql;
// construct
function mysql_session_handler() {
// read the maxlifetime setting from PHP
mysql_session_handler::$life_time = get_cfg_var('session.gc_maxlifetime');
// register this object as the session handler
session_set_save_handler(
array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc')
);
if(mysql_session_handler::$mysql == null) {
// create a new database connection or use the already opened
mysql_session_handler::$mysql = new classDataBase();
mysql_session_handler::$mysql->Init('db', 'ip', 'user', 'password');
}
}
// open session -> just to have. no action
function open($save_path, $session_name) {
global $sess_save_path; $sess_save_path = $save_path;
return true;
}
// read session data
function read($id) {
$data = '';
$qry = mysql_session_handler::$mysql->query("select session_data
from cache_sessions
where session_id = '".mysql_escape_string($id)."'
and session_expires >= ".time());
while($fetch = mysql_session_handler::$mysql->fetchArray($qry)) {
$data = $fetch[0];
}
return $data;
}
// insert / update session information
function write($id, $data) {
mysql_session_handler::$mysql->query("replace into cache_sessions
(session_id,
session_data,
session_expires)
values ('".mysql_escape_string($id)."',
'".mysql_escape_string($data)."',
".(time() + mysql_session_handler::$life_time).")");
return true;
}
// destroy a session
function destroy($id) {
mysql_session_handler::$mysql->query("delete from cache_sessions where session_id = '".mysql_escape_string($id)."'");
return true;
}
// clean old session
function gc() {
// do not clean by an page call, use a cronjob to delete all expired entries every minute!
/*mysql_session_handler::$mysql->query('delete from cache_sessions where session_expires < '.time());*/
return true;
}
// close session
function close() {
return true;
}
}
$SessionHandler = new mysql_session_handler();
}