You are here

function _drupal_session_read in Memcache Storage 7

Reads an entire session from the memcached.

Also initializes the $user object for the user associated with the session. This function is registered with session_set_save_handler() to support memcached-backed sessions. It is called on every page load when PHP sets up the $_SESSION superglobal.

This function is an internal function and must not be called directly. Doing so may result in logging out the current user, corrupting session data or other unexpected behavior. Session data must always be accessed via the $_SESSION superglobal.

Parameters

$sid: The session ID of the session to retrieve.

Return value

string The user's session, or an empty string if no session exists.

1 string reference to '_drupal_session_read'
drupal_session_initialize in includes/session.inc
Initializes the session handler, starting a session if needed.

File

includes/session.inc, line 75
User session handling functions.

Code

function _drupal_session_read($sid) {
  global $user, $is_https;

  // Write and Close handlers are called after destructing objects
  // since PHP 5.0.5.
  // Thus destructors can use sessions but session handler can't use objects.
  // So we are moving session closure before destructing objects.
  drupal_register_shutdown_function('session_write_close');

  // Handle the case of first time visitors and clients that don't store
  // cookies (eg. web crawlers).
  $insecure_session_name = substr(session_name(), 1);
  if (!isset($_COOKIE[session_name()]) && !isset($_COOKIE[$insecure_session_name])) {
    $user = drupal_anonymous_user();
    return '';
  }
  if ($is_https && isset($_COOKIE[$insecure_session_name])) {
    $session = MemcacheStorageAPI::get($_COOKIE[$insecure_session_name], 'sessions');
  }
  else {
    $session = MemcacheStorageAPI::get($sid, 'sessions');
  }
  $user = _memcache_storage_session_user_load($session);

  // Store the session that was read for comparison in _drupal_session_write().
  $last_read =& drupal_static('drupal_session_last_read');
  $last_read = array(
    'sid' => $sid,
    'value' => $user->session,
  );
  return $user->session;
}