Skip to content
Snippets Groups Projects
Commit 3ace12ca authored by Dries Buytaert's avatar Dries Buytaert
Browse files

- Patch #node/76931 by Robert: improved performance of Drupal's session handling.

parent 9f8abbcc
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -610,6 +610,20 @@ function drupal_is_denied($type, $mask) { ...@@ -610,6 +610,20 @@ function drupal_is_denied($type, $mask) {
return $deny && !$allow; return $deny && !$allow;
} }
/**
* Generates a default annonymous $user object.
*
* @return Object - the user object.
*/
function drupal_anonymous_user() {
$user = new stdClass();
$user->uid = 0;
$user->hostname = $_SERVER['REMOTE_ADDR'];
$user->roles = array();
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user';
return $user;
}
/** /**
* A string describing a phase of Drupal to load. Each phase adds to the * A string describing a phase of Drupal to load. Each phase adds to the
* previous one, so invoking a later phase automatically runs the earlier * previous one, so invoking a later phase automatically runs the earlier
......
...@@ -17,39 +17,44 @@ function sess_close() { ...@@ -17,39 +17,44 @@ function sess_close() {
function sess_read($key) { function sess_read($key) {
global $user; global $user;
// retrieve data for a $user object // Handle the case of first time visitors and clients that don't store cookies (eg. web crawlers).
$result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key); if (!isset($_COOKIE[session_name()])) {
if (!db_num_rows($result)) { $user = drupal_anonymous_user();
$result = db_query("SELECT u.* FROM {users} u WHERE u.uid = 0"); return '';
}
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: // Otherwise, if the session is still active, we have a record of the client's session in the database.
$user = db_fetch_object($result); $user = db_fetch_object(db_query("SELECT u.*, s.* FROM {users} u INNER JOIN {sessions} s ON u.uid = s.uid WHERE s.sid = '%s'", $key));
$user = drupal_unpack($user);
// Add roles element to $user: // We found the client's session record and they are an authenticated user
$user->roles = array(); if ($user->uid > 0) {
if ($user->uid) { // This is done to unserialize the data member of $user
$user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user'; $user = drupal_unpack($user);
// Add roles element to $user
$user->roles = array();
$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); $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)) { while ($role = db_fetch_object($result)) {
$user->roles[$role->rid] = $role->name; $user->roles[$role->rid] = $role->name;
} }
} }
else { // We didn't find the client's record (session has expired), or they are an anonymous user.
$user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user'; else {
$user = drupal_anonymous_user();
} }
return !empty($user->session) ? $user->session : ''; return $user->session;
} }
function sess_write($key, $value) { function sess_write($key, $value) {
global $user; global $user;
// If the client doesn't have a session, and one isn't being created ($value), do nothing.
if (empty($_COOKIE[session_name()]) && empty($value)) {
return TRUE;
}
$result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key); $result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key);
if (!db_num_rows($result)) { if (!db_num_rows($result)) {
...@@ -87,5 +92,4 @@ function sess_gc($lifetime) { ...@@ -87,5 +92,4 @@ function sess_gc($lifetime) {
db_query("DELETE FROM {sessions} WHERE timestamp < %d", time() - $lifetime); db_query("DELETE FROM {sessions} WHERE timestamp < %d", time() - $lifetime);
return TRUE; return TRUE;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment