Commit 37ec1bca authored by Dries's avatar Dries

- Patch #44947 by rkerr / Moshe: fixed race condition in session handling,...

- Patch #44947 by rkerr / Moshe: fixed race condition in session handling, reduced query overhead of session handling, reduced database overhead of session handling.
parent 583d5a4e
......@@ -608,7 +608,7 @@ function watchdog($type, $message, $severity = WATCHDOG_NOTICE, $link = NULL) {
* - 'error'
*/
function drupal_set_message($message = NULL, $type = 'status') {
if (isset($message)) {
if ($message) {
if (!isset($_SESSION['messages'])) {
$_SESSION['messages'] = array();
}
......@@ -631,7 +631,7 @@ function drupal_set_message($message = NULL, $type = 'status') {
*/
function drupal_get_messages() {
$messages = drupal_set_message();
$_SESSION['messages'] = array();
unset($_SESSION['messages']);
return $messages;
}
......
......@@ -6,40 +6,43 @@
* User session handling functions.
*/
/*** Session functions *****************************************************/
function sess_open($save_path, $session_name) {
return 1;
return TRUE;
}
function sess_close() {
return 1;
return TRUE;
}
function sess_read($key) {
global $user;
$result = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = '%s'", $key);
// retrieve data for a $user object
$result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key);
if (!db_num_rows($result)) {
db_query("INSERT INTO {sessions} (sid, uid, hostname, timestamp) VALUES ('%s', 0, '%s', %d)", $key, $_SERVER["REMOTE_ADDR"], time());
$result = db_query("SELECT u.* FROM {users} u WHERE u.uid = 0");
}
else {
$result = db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = '%s'", $key);
}
// Build $user object:
$user = db_fetch_object($result);
$user = drupal_unpack($user);
$user->roles = array();
// Add roles element to $user:
$user->roles = array();
if ($user->uid) {
$user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user';
$result = db_query("SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = %d", $user->uid);
while ($role = db_fetch_object($result)) {
$user->roles[$role->rid] = $role->name;
}
}
else {
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
}
$result = db_query("SELECT r.rid, r.name FROM {role} r INNER JOIN {users_roles} ur ON ur.rid = r.rid WHERE ur.uid = %d", $user->uid);
while ($role = db_fetch_object($result)) {
$user->roles[$role->rid] = $role->name;
}
return !empty($user->session) ? $user->session : '';
}
......@@ -47,11 +50,24 @@ function sess_read($key) {
function sess_write($key, $value) {
global $user;
db_query("UPDATE {sessions} SET uid = %d, cache = %d, hostname = '%s', session = '%s', timestamp = %d WHERE sid = '%s'", $user->uid, $user->cache, $_SERVER["REMOTE_ADDR"], $value, time(), $key);
if ($user->uid) {
db_query("UPDATE {users} SET access = %d WHERE uid = %d", time(), $user->uid);
$result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key);
if (!db_num_rows($result)) {
// Only save session data when there is session data available.
if ($user->uid || $value) {
db_query("INSERT INTO {sessions} (sid, uid, cache, hostname, session, timestamp) VALUES ('%s', %d, %d, '%s', '%s', %d)", $key, $user->uid, $user->cache, $_SERVER["REMOTE_ADDR"], $value, time());
}
}
else {
db_query("UPDATE {sessions} SET uid = %d, cache = %d, hostname = '%s', session = '%s', timestamp = %d WHERE sid = '%s'", $user->uid, $user->cache, $_SERVER["REMOTE_ADDR"], $value, time(), $key);
// TODO: this can be an expensive query. Perhaps only execute it every x minutes. Requires investigation into cache expiration.
if ($user->uid) {
db_query("UPDATE {users} SET access = %d WHERE uid = %d", time(), $user->uid);
}
}
return '';
return TRUE;
}
function sess_destroy($key) {
......@@ -59,18 +75,13 @@ function sess_destroy($key) {
}
function sess_gc($lifetime) {
/*
** Be sure to adjust 'php_value session.gc_maxlifetime' to a large enough
** value. For example, if you want user sessions to stay in your database
** for three weeks before deleting them, you need to set gc_maxlifetime
** to '1814400'. At that value, only after a user doesn't log in after
** three weeks (1814400 seconds) will his/her session be removed.
*/
// Be sure to adjust 'php_value session.gc_maxlifetime' to a large enough
// value. For example, if you want user sessions to stay in your database
// for three weeks before deleting them, you need to set gc_maxlifetime
// to '1814400'. At that value, only after a user doesn't log in after
// three weeks (1814400 seconds) will his/her session be removed.
db_query("DELETE FROM {sessions} WHERE timestamp < %d", time() - $lifetime);
return 1;
return TRUE;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment