| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: session.inc,v 1.44.2.7 2010/03/04 00:15:28 goba Exp $ 3 4 /** 5 * @file 6 * User session handling functions. 7 */ 8 9 function sess_open($save_path, $session_name) { 10 return TRUE; 11 } 12 13 function sess_close() { 14 return TRUE; 15 } 16 17 function sess_read($key) { 18 global $user; 19 20 // Write and Close handlers are called after destructing objects since PHP 5.0.5 21 // Thus destructors can use sessions but session handler can't use objects. 22 // So we are moving session closure before destructing objects. 23 register_shutdown_function('session_write_close'); 24 25 // Handle the case of first time visitors and clients that don't store cookies (eg. web crawlers). 26 if (!isset($_COOKIE[session_name()])) { 27 $user = drupal_anonymous_user(); 28 return ''; 29 } 30 31 // Otherwise, if the session is still active, we have a record of the client's session in the database. 32 $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)); 33 34 // We found the client's session record and they are an authenticated, 35 // active user. 36 if ($user && $user->uid > 0 && $user->status == 1) { 37 // This is done to unserialize the data member of $user 38 $user = drupal_unpack($user); 39 40 // Add roles element to $user 41 $user->roles = array(); 42 $user->roles[DRUPAL_AUTHENTICATED_RID] = 'authenticated user'; 43 $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); 44 while ($role = db_fetch_object($result)) { 45 $user->roles[$role->rid] = $role->name; 46 } 47 } 48 // We didn't find the client's record (session has expired), or they are 49 // blocked, or they are an anonymous user. 50 else { 51 $session = isset($user->session) ? $user->session : ''; 52 $user = drupal_anonymous_user($session); 53 } 54 55 return $user->session; 56 } 57 58 function sess_write($key, $value) { 59 global $user; 60 61 // If saving of session data is disabled or if the client doesn't have a session, 62 // and one isn't being created ($value), do nothing. This keeps crawlers out of 63 // the session table. This reduces memory and server load, and gives more useful 64 // statistics. We can't eliminate anonymous session table rows without breaking 65 // the throttle module and the "Who's Online" block. 66 if (!session_save_session() || ($user->uid == 0 && empty($_COOKIE[session_name()]) && empty($value))) { 67 return TRUE; 68 } 69 70 db_query("UPDATE {sessions} SET uid = %d, cache = %d, hostname = '%s', session = '%s', timestamp = %d WHERE sid = '%s'", $user->uid, isset($user->cache) ? $user->cache : '', ip_address(), $value, time(), $key); 71 if (db_affected_rows()) { 72 // Last access time is updated no more frequently than once every 180 seconds. 73 // This reduces contention in the users table. 74 if ($user->uid && time() - $user->access > variable_get('session_write_interval', 180)) { 75 db_query("UPDATE {users} SET access = %d WHERE uid = %d", time(), $user->uid); 76 } 77 } 78 else { 79 // If this query fails, another parallel request probably got here first. 80 // In that case, any session data generated in this request is discarded. 81 @db_query("INSERT INTO {sessions} (sid, uid, cache, hostname, session, timestamp) VALUES ('%s', %d, %d, '%s', '%s', %d)", $key, $user->uid, isset($user->cache) ? $user->cache : '', ip_address(), $value, time()); 82 } 83 84 return TRUE; 85 } 86 87 /** 88 * Called when an anonymous user becomes authenticated or vice-versa. 89 */ 90 function sess_regenerate() { 91 $old_session_id = session_id(); 92 93 // We code around http://bugs.php.net/bug.php?id=32802 by destroying 94 // the session cookie by setting expiration in the past (a negative 95 // value). This issue only arises in PHP versions before 4.4.0, 96 // regardless of the Drupal configuration. 97 // TODO: remove this when we require at least PHP 4.4.0 98 if (isset($_COOKIE[session_name()])) { 99 setcookie(session_name(), '', time() - 42000, '/'); 100 } 101 102 session_regenerate_id(); 103 104 db_query("UPDATE {sessions} SET sid = '%s' WHERE sid = '%s'", session_id(), $old_session_id); 105 } 106 107 /** 108 * Counts how many users have sessions. Can count either anonymous sessions or authenticated sessions. 109 * 110 * @param int $timestamp 111 * A Unix timestamp representing a point of time in the past. 112 * The default is 0, which counts all existing sessions. 113 * @param boolean $anonymous 114 * TRUE counts only anonymous users. 115 * FALSE counts only authenticated users. 116 * @return int 117 * The number of users with sessions. 118 */ 119 function sess_count($timestamp = 0, $anonymous = true) { 120 $query = $anonymous ? ' AND uid = 0' : ' AND uid > 0'; 121 return db_result(db_query('SELECT COUNT(sid) AS count FROM {sessions} WHERE timestamp >= %d'. $query, $timestamp)); 122 } 123 124 /** 125 * Called by PHP session handling with the PHP session ID to end a user's session. 126 * 127 * @param string $sid 128 * the session id 129 */ 130 function sess_destroy_sid($sid) { 131 db_query("DELETE FROM {sessions} WHERE sid = '%s'", $sid); 132 } 133 134 /** 135 * End a specific user's session 136 * 137 * @param string $uid 138 * the user id 139 */ 140 function sess_destroy_uid($uid) { 141 db_query('DELETE FROM {sessions} WHERE uid = %d', $uid); 142 } 143 144 function sess_gc($lifetime) { 145 // Be sure to adjust 'php_value session.gc_maxlifetime' to a large enough 146 // value. For example, if you want user sessions to stay in your database 147 // for three weeks before deleting them, you need to set gc_maxlifetime 148 // to '1814400'. At that value, only after a user doesn't log in after 149 // three weeks (1814400 seconds) will his/her session be removed. 150 db_query("DELETE FROM {sessions} WHERE timestamp < %d", time() - $lifetime); 151 152 return TRUE; 153 } 154 155 /** 156 * Determine whether to save session data of the current request. 157 * 158 * This function allows the caller to temporarily disable writing of session data, 159 * should the request end while performing potentially dangerous operations, such as 160 * manipulating the global $user object. See http://drupal.org/node/218104 for usage 161 * 162 * @param $status 163 * Disables writing of session data when FALSE, (re-)enables writing when TRUE. 164 * @return 165 * FALSE if writing session data has been disabled. Otherwise, TRUE. 166 */ 167 function session_save_session($status = NULL) { 168 static $save_session = TRUE; 169 if (isset($status)) { 170 $save_session = $status; 171 } 172 return ($save_session); 173 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |