You can modify and use this class, it keeps session in DB
class MySQLSession
{
protected $resource = null;
protected $db = 'base';
protected $table = 'session';
protected $id_col = 'sess_id';
protected $data_col = 'sess_data';
protected $time_col = 'sess_time';
public function __construct($context, $parameters = null){
session_set_save_handler(array($this, 'sessionOpen'),
array($this, 'sessionClose'),
array($this, 'sessionRead'),
array($this, 'sessionWrite'),
array($this, 'sessionDestroy'),
array($this, 'sessionGC'));
session_start();
}
public function sessionClose(){
return true;
}
public function sessionDestroy($id){
$id = mysql_real_escape_string($id, $this->resource);
$sql = 'DELETE FROM '.$this->table.' WHERE '.$this->id_col.' = \''.$id.'\'';
if (@mysql_query($sql, $this->resource)){
return true;
}
$error = 'MySQLSessionStorage cannot destroy session id "%s"';
$error = sprintf($error, $id);
throw new Exception($error);
}
public function sessionGC($lifetime){
$sql = 'DELETE FROM '.$this->table.' '.
'WHERE '.$this->time_col.' + INTERVAL '.$lifetime.' SECOND < NOW()';
if (@mysql_query($sql, $this->resource)){
return true;
}
$error = 'MySQLSessionStorage cannot delete old sessions';
throw new Exception($error);
}
public function sessionOpen($path, $name){
$this->resource = mysql_connect('host', 'user', 'pass', true);
mysql_select_db($this->db, $this->resource);
return true;
}
public function sessionRead($id){
$id = mysql_real_escape_string($id, $this->resource);
$sql = 'SELECT '.$this->data_col.' ' .
'FROM '.$this->table.' ' .
'WHERE '.$this->id_col.' = \''.$id.'\'';
$result = @mysql_query($sql, $this->resource);
if ($result != false && @mysql_num_rows($result) == 1){
$data = mysql_fetch_row($result);
return $data[0];
}else{
$sql = 'INSERT INTO '.$this->table.' ('.$this->id_col.', ' .
$this->data_col.', '.$this->time_col.') VALUES (' .
'\''.$id.'\', \'\', NOW())';
if (@mysql_query($sql, $this->resource)){
return '';
}
$error = 'MySQLSessionStorage cannot create new record for id "%s"';
$error = sprintf($error, $id);
throw new Exception($error);
}
}
public function sessionWrite($id, &$data){
$id = mysql_real_escape_string($id, $this->resource);
$data = mysql_real_escape_string($data, $this->resource);
$sql = 'UPDATE '.$this->table.' ' .
'SET '.$this->data_col.' = \''.$data.'\', ' .
$this->time_col.' = NOW() ' .
'WHERE '.$this->id_col.' = \''.$id.'\'';
if (@mysql_query($sql, $this->resource)){
return true;
}
$error = 'MySQLSessionStorage cannot write session data for id "%s"';
$error = sprintf($error, $id);
throw new Exception($error);
}
public function shutdown(){
}
}