PHP Code:
class session
{
var $connection;
var $formersid;
var $user_agent;
var $ip_address;
var $referring;
function session($conn = NULL, $browser = '', $ip = '', $refer = '') {
$this->connection = $conn;
$this->user_agent = $this->connection->filter($browser);
$this->ip_address = $this->connection->filter($ip);
$this->referring = $this->connection->filter($refer);
session_set_save_handler(
array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc')
);
$this->set();
$this->start();
$this->noFixation();
}
function set() {
ini_set('session.name', 's');
ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 0);
ini_set('session.gc_probability', 1);
ini_set('session.gc_maxlifetime', 3600);
}
function start() {
if(!isset($_SESSION)) {
session_start();
} else {
session_destroy();
session_start();
$this->_msg('session', 'should not start a session outside the session object');
}
}
function noFixation() {
$this->formersid = session_id();
session_regenerate_id();
}
function open($save, $session) {
if(is_object($this->connection)) {
return true;
} else {
$this->_msg('session', 'connection is not an object');
return false;
}
if(is_resource($this->connection->getConn())) {
return true;
} else {
$this->_msg('session', 'connection resource is invalid');
return false;
}
}
function close() {
$this->connection->query("DELETE FROM sessions WHERE sesskey = '".$this->formersid."'", false);
return $this->connection->disconnect();
}
function read($id) {
$result = $this->connection->query("SELECT * FROM sessions WHERE sesskey = '".$this->connection->filter($id)."' AND sessvalid = 'yes'");
$row = $result->getCurrent();
if($result->getRowCount()) {
if($this->user_agent && $this->user_agent != $row['sessagent']) {
$this->_msg('session', 'there is a problem identifying you with the current session, please login again');
return '';
}
if($this->referring && $this->referring != $row['sessrefer']) {
$this->_msg('session', 'an error in page traversal has occurred');
return '';
}
if($this->ip_address) {
$this->ip_address = str_replace('.*', '', $this->ip_address);
if($this->ip_address == substr($row['sessip'], 0, strlen($this->ip_address))) return $row['sessdata'];
$this->_msg('session', 'there is a problem identifying you with the current session, please login again');
return '';
}
return $row['sessdata'];
}
return '';
}
function write($id, $data) {
$result = $this->connection->query("SELECT * FROM sessions WHERE sesskey = '$id' AND sessvalid = 'yes'");
$row = $result->getCurrent();
if($result->getRowCount()) {
if($this->user_agent && $this->user_agent != $row['sessagent']) {
$this->_msg('session', 'there is a problem identifying you with the current session, please login again');
return false;
}
if($this->referring && $this->referring != $row['sessrefer']) {
$this->_msg('session', 'an error in page traversal has occurred');
return false;
}
if($this->ip_address) {
$this->ip_address = str_replace('.*', '', $this->ip_address);
if($this->ip_address == substr($row['sessip'], 0, strlen($this->ip_address))) return true;
$this->_msg('session', 'there is a problem identifying you with the current session, please login again');
return false;
}
}
$result = $this->connection->query("INSERT INTO sessions (sessexp, sessvalid, sesskey, sessdata, sessagent, sessip, sessrefer) VALUES (".
time().", 'yes', '".$this->connection->filter($id)."', '".$this->connection->filter($data).
"', '$this->user_agent', '$this->ip_address', '$this->referring')");
return $result->isSuccess();
}
function destroy($id) {
$result = $this->connection->query("UPDATE sessions SET sessvalid = 'no' WHERE sesskey = '".$this->connection->filter($id)."'");
return $result->isSuccess();
}
function gc($lifetime) {
$result = $this->connection->query("UPDATE sessions SET sessvalid = 'no' WHERE sessvalid = 'yes' AND sessexp < ".time() - $lifetime);
return $result->isSuccess();
}
function _msg($type, $msg) {
trigger_error($type.'|'.$msg);
}
}
Bookmarks