session.inc

User session handling functions.

Archivo

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

Functions

Nombreorden descendente Descripción
session_save_session Determine whether to save session data of the current request.
sess_close
sess_count Counts how many users have sessions. Can count either anonymous sessions or authenticated sessions.
sess_destroy_sid Called by PHP session handling with the PHP session ID to end a user's session.
sess_destroy_uid End a specific user's session
sess_gc
sess_open
sess_read Reads an entire session from the database (internal use only).
sess_regenerate Called when an anonymous user becomes authenticated or vice-versa.
sess_write Writes an entire session to the database (internal use only).