From 3ace12caf8f4d46fda44b79b4a3cbe91b0bca5c0 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Wed, 16 Aug 2006 13:13:34 +0000 Subject: [PATCH] - Patch #node/76931 by Robert: improved performance of Drupal's session handling. --- includes/bootstrap.inc | 14 ++++++++++++++ includes/session.inc | 42 +++++++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index 5a6c6258073..1aa6e8d2698 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -610,6 +610,20 @@ function drupal_is_denied($type, $mask) { 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 * previous one, so invoking a later phase automatically runs the earlier diff --git a/includes/session.inc b/includes/session.inc index 160376d8414..42f5e826b18 100644 --- a/includes/session.inc +++ b/includes/session.inc @@ -17,39 +17,44 @@ function sess_close() { function sess_read($key) { global $user; - // retrieve data for a $user object - $result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key); - if (!db_num_rows($result)) { - $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); + // Handle the case of first time visitors and clients that don't store cookies (eg. web crawlers). + if (!isset($_COOKIE[session_name()])) { + $user = drupal_anonymous_user(); + return ''; } - // Build $user object: - $user = db_fetch_object($result); - $user = drupal_unpack($user); + // Otherwise, if the session is still active, we have a record of the client's session in the database. + $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)); - // Add roles element to $user: - $user->roles = array(); - if ($user->uid) { + // We found the client's session record and they are an authenticated user + if ($user->uid > 0) { + // This is done to unserialize the data member of $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); while ($role = db_fetch_object($result)) { $user->roles[$role->rid] = $role->name; } } - else { - $user->roles[DRUPAL_ANONYMOUS_RID] = 'anonymous user'; + // We didn't find the client's record (session has expired), or they are an anonymous user. + else { + $user = drupal_anonymous_user(); } - return !empty($user->session) ? $user->session : ''; + return $user->session; } function sess_write($key, $value) { 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); if (!db_num_rows($result)) { @@ -87,5 +92,4 @@ function sess_gc($lifetime) { db_query("DELETE FROM {sessions} WHERE timestamp < %d", time() - $lifetime); return TRUE; -} - +} \ No newline at end of file