<?php
require_once('lib/Mapper.php');
class Session extends Mapper {
protected $_pKey = 'session_id';
protected $_table = 'sessions';
protected $_columns = array('session_id', 'session_data', 'expires', 'updated');
protected $_session_id = NULL;
protected $_lifetime = NULL;
protected $_acl = NULL;
public function __construct(Database $db, $session_id = NULL){
parent::__construct($db);
$this->_session_id = $session_id;
// Read the maxlifetime setting from PHP
$this->_lifetime = ini_get("session.gc_maxlifetime");
}
public function __destruct(){
session_write_close();
}
public function open($save_path, $session_id){
return true;
}
public function read($session_id){
$data = $this->find(array('session_id' => $session_id));
if(!empty($data[0]))
$session = $data[0]['session_data'];
return (!empty($session)) ? $session : '';
}
public function write($session_id, $data){
// where does the session data come from??? set it in the code in the auth and login!!!
$bind = array('session_id' => $session_id, 'session_data' => $data, 'expires' => REQUEST_TIME + $this->_lifetime, 'updated' => REQUEST_TIME);
if($this->replace($bind)){
return true;
}
return false;
}
public function close(){
return true;
}
public function destroy($session_id){
$this->remove($session_id);
return true;
}
public function gc(){
$this->_db->query('DELETE FROM '.$this->_table.' WHERE expires < '.(REQUEST_TIME + $this->_lifetime));
return true;
}
public function setId($id){
$this->_session_id = $id;
}
public function getId(){
return $this->_session_id;
}
public function find($params = array(), $order = array(), $skip = 0, $limit = NULL){
return $this->_finder($params, $order, $skip, $limit);
}
}
$Session = new Session($_db);
// Register this object as the session handler
session_set_save_handler(
array( $Session, "open" ),
array( $Session, "close" ),
array( $Session, "read" ),
array( $Session, "write"),
array( $Session, "destroy"),
array( $Session, "gc" )
);
session_start();
register_shutdown_function('session_write_close');