Implement a Session class that takes over session functions and stores in the database
This commit is contained in:
parent
3e668b43cf
commit
5d20dde85c
|
@ -87,4 +87,71 @@ function zm_session_clear() {
|
|||
session_write_close();
|
||||
session_start();
|
||||
} // function zm_session_clear()
|
||||
|
||||
|
||||
class Session {
|
||||
private $db;
|
||||
public function __construct() {
|
||||
global $dbConn;
|
||||
$this->db = $dbConn;
|
||||
|
||||
// Set handler to overide SESSION
|
||||
session_set_save_handler(
|
||||
array($this, '_open'),
|
||||
array($this, '_close'),
|
||||
array($this, '_read'),
|
||||
array($this, '_write'),
|
||||
array($this, '_destroy'),
|
||||
array($this, '_gc')
|
||||
);
|
||||
|
||||
// Start the session
|
||||
//zm_session_start();
|
||||
}
|
||||
public function _open() {
|
||||
return $this->db ? true : false;
|
||||
}
|
||||
public function _close(){
|
||||
// The example code closed the db connection.. I don't think we care to.
|
||||
return true;
|
||||
}
|
||||
public function _read($id){
|
||||
ZM\Logger::Debug("read session" . ($this->db ? true : false));
|
||||
$sth = $this->db->prepare('SELECT data FROM Sessions WHERE id = :id');
|
||||
$sth->bindParam(':id', $id, PDO::PARAM_STR, 32);
|
||||
|
||||
if ( $sth->execute() and ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) ) {
|
||||
ZM\Logger::Debug("row: " . print_r($row,true));
|
||||
return $row['data'];
|
||||
}
|
||||
// Return an empty string
|
||||
return '';
|
||||
}
|
||||
public function _write($id, $data){
|
||||
// Create time stamp
|
||||
$access = time();
|
||||
|
||||
$sth = $this->db->prepare('REPLACE INTO Sessions VALUES (:id, :access, :data)');
|
||||
|
||||
$sth->bindParam(':id', $id, PDO::PARAM_STR, 32);
|
||||
$sth->bindParam(':access', $access, PDO::PARAM_INT);
|
||||
$sth->bindParam(':data', $data);
|
||||
|
||||
return $sth->execute() ? true : false;
|
||||
}
|
||||
public function _destroy($id) {
|
||||
$sth = $this->db->prepare('DELETE FROM Sessions WHERE Id = :id');
|
||||
$sth->bindParam(':id', $id, PDO::PARAM_STR, 32);
|
||||
return $sth->execute() ? true : false;
|
||||
}
|
||||
public function _gc($max) {
|
||||
// Calculate what is to be deemed old
|
||||
$old = time() - $max;
|
||||
$sth = $this->db->prepare('DELETE * FROM Sessions WHERE access < :old');
|
||||
$sth->bindParam(':old', $old, PDO::PARAM_INT);
|
||||
return $sth->execute() ? true : false;
|
||||
}
|
||||
} # end class Session
|
||||
|
||||
$session = new Session;
|
||||
?>
|
||||
|
|
Loading…
Reference in New Issue