Database.php in Session Proxy 7
File
lib/SessionProxy/Storage/Database.php
View source
<?php
class SessionProxy_Storage_Database extends SessionProxy_Storage_Base {
public function open() {
return TRUE;
}
public function close() {
return TRUE;
}
public function read($sessionId) {
global $user;
if (!isset($_COOKIE[$this->sessionName])) {
return '';
}
$data = db_query("SELECT u.uid, u.status, s.session FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = :sid", array(
':sid' => $sessionId,
))
->fetchObject();
if ($data && $data->uid > 0 && $data->status == 1) {
$this->uid = $data->uid;
$serializedData = $data->session;
}
else {
$serializedData = '';
}
$this
->sessionDataSetHash($sessionId, $serializedData);
return $serializedData;
}
public function write($sessionId, $serializedData) {
try {
if ($this
->sessionDataHasChanged($sessionId, $serializedData)) {
$fields = array(
'uid' => $this->uid,
'cache' => 0,
'hostname' => ip_address(),
'session' => $serializedData,
'timestamp' => REQUEST_TIME,
);
$key = array(
'sid' => $sessionId,
'ssid' => '',
);
db_merge('sessions')
->key($key)
->fields($fields)
->execute();
}
return TRUE;
} catch (Exception $exception) {
require_once DRUPAL_ROOT . '/includes/errors.inc';
if (error_displayable()) {
print '<h1>Uncaught exception thrown in session handler.</h1>';
print '<p>' . _drupal_render_exception_safe($exception) . '</p><hr />';
}
return FALSE;
}
}
public function destroy($sessionId) {
db_delete('sessions')
->condition('sid', $sessionId)
->execute();
$_SESSION = array();
$this
->setSessionUid(NULL);
return TRUE;
}
public function gc($lifetime) {
db_delete('sessions')
->condition('timestamp', REQUEST_TIME - $lifetime, '<')
->execute();
return TRUE;
}
public function handleHttps() {
return FALSE;
}
public function destroyFor($index, $value) {
if ('uid' == $index) {
db_delete('sessions')
->condition($index, $value)
->execute();
}
}
}