Hallo,
wir haben hier um die Sessions zu handeln ne eigene Klasse gebaut, nur leider will die die ausgelaufenen Sessions nicht aus der DB nehmen...
SessionDbUser::_hdlGC() löscht die abgelaufenen Sessions!
wir haben hier um die Sessions zu handeln ne eigene Klasse gebaut, nur leider will die die ausgelaufenen Sessions nicht aus der DB nehmen...
PHP-Code:
class SessionDbUser extends Session {
var $_rDB;
var $_wDB;
function SessionDbUser($db) {
$this->_rDB = $db;
$this->_wDB = $db;
if (session_id() != '') session_write_close();
register_shutdown_function('session_write_close');
session_set_save_handler( array(&$this, '_hdlOpen'),
array(&$this, '_hdlClose'),
array(&$this, '_hdlRead'),
array(&$this, '_hdlWrite'),
array(&$this, '_hdlDestroy'),
array(&$this, '_hdlGc')
);
$this->Session();
}
function getVisitorCnt() {
$result = $this->_rDB->query('SELECT COUNT(*) as cnt FROM sessions WHERE user = 0');
if (!$result->isValid() || !($row = $result->getNextRow())) return FALSE;
return $row[0];
}
function getMemberCnt() {
$result = $this->_rDB->query('SELECT COUNT(*) as cnt FROM sessions WHERE user != 0');
if (!$result->isValid() || !($row = $result->getNextRow())) return FALSE;
return $row[0];
}
function _hdlOpen() {
// $this->_hdlGc(0);
}
function _hdlClose() {
}
function _hdlRead($id) {
$query = 'SELECT data FROM sessions WHERE id=\'%1$s\' LIMIT 1';
$result = $this->_rDB->query(sprintf($query, $this->_rDB->escape($id)));
if (!$result->isValid() || !($row = $result->getNextRow())) return '';
return $row[0];
}
function _hdlWrite($id, $data) {
$query = 'SELECT data FROM sessions WHERE id=\'%1$s\' LIMIT 1';
$result = $this->_rDB->query(sprintf($query, $this->_rDB->escape($id)));
if (!$result->isValid()) return FALSE;
$user = isset($GLOBALS['AUTH']) ? $GLOBALS['AUTH']->getUserId() : 0;
if ($result->count() != 1) {
$query = 'INSERT INTO sessions (id, data, user, time, start) VALUES(\'%1$s\', \'%2$s\', \'%3$d\', \'%4$d\', \'%5$d\')';
$result = $this->_wDB->query(sprintf($query, $this->_wDB->escape($id),
$this->_wDB->escape($data),
$this->_wDB->escape($user),
$this->_wDB->escape(time()),
$this->_wDB->escape(time())
));
return $result->isValid();
}
$query = 'UPDATE sessions SET data=\'%2$s\', user=\'%3$d\', time=\'%4$d\' WHERE id=\'%1$s\' LIMIT 1';
$result = $this->_wDB->query(sprintf($query, $this->_wDB->escape($id),
$this->_wDB->escape($data),
$this->_wDB->escape($user),
$this->_wDB->escape(time())
));
return $result->isValid();
}
function _hdlDestroy($id) {
$query = 'DELETE FROM sessions WHERE id=\'%1$s\' LIMIT 1';
$result = $this->_wDB->query(sprintf($query, $this->_wDB->escape($id)));
return $result->isValid();
}
function _hdlGc($maxlifetime) {
$query = 'DELETE FROM sessions WHERE time < \'%1$d\'';
$result = $this->_wDB->query(sprintf($query, $this->_wDB->escape(time() - $maxlifetime)));
return $result->isValid();
}
}
Kommentar