You are here

function _drupal_session_write in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 modules/memcache/unstable/memcache-session.inc \_drupal_session_write()

Write a session to session storage.

We have the following cases to handle. 1. Anonymous user 1a. Without session data. 1b. With session data. 1c. Session saving has been turned off programatically (see drupal_save_session()). 1d. Without session data but had session data at the beginning of the request (thus a write must be made to clear stored session data). 2. Authenticated user. 2a. Without session data. 2b. With session data. 2c. Session saving has been turned off programatically (see drupal_save_session()).

Parameters

$key: The session ID.

$value: Any data to store in the session.

Return value

TRUE.

1 string reference to '_drupal_session_write'
drupal_session_initialize in modules/memcache/unstable/memcache-session.inc
Initialize the session handler, starting a session if needed.

File

modules/memcache/unstable/memcache-session.inc, line 86

Code

function _drupal_session_write($key, $value) {
  global $user;
  if (!drupal_save_session()) {
    return TRUE;
  }

  // Prepare the information to be saved.
  $session = new stdClass();
  $session->sid = $key;
  $session->uid = $user->uid;
  $session->cache = isset($user->cache) ? $user->cache : '';
  $session->hostname = ip_address();
  $session->session = $value;
  $session->timestamp = REQUEST_TIME;

  // Be sure that we have the latest user object.  If user_save() has been
  // called, we need to refresh the object from the database.
  $user = _memcache_session_user_load($session);

  // If this is an authenticated user, or there is something to save in the
  // session, or this is an anonymous user who currently has nothing in the
  // session but did have something in session storage, write it to memcache.
  // If $user->session_data_present_at_load is not set, the current user
  // was created during this request and it's safest to do a write.
  // Cases 1b, 1d, 2a, and 2b are covered here.
  if ($user->uid || !empty($value) || empty($value) && (!isset($user->session_data_present_at_load) || $user->session_data_present_at_load)) {
    dmemcache_set($key, $session, ini_get('session.gc_maxlifetime'), 'session');
    if ($user->uid && $session->timestamp - $user->access > variable_get('session_write_interval', 360)) {
      db_update('users')
        ->fields(array(
        'access' => $session->timestamp,
      ))
        ->condition('uid', $user->uid)
        ->execute();

      // Update the user access time so that the dmemcache_set() call
      // caches the updated time.
      $user->access = $session->timestamp;
    }

    // Data stored in session is stored in session memcache; no need
    // to duplicate it in users memcache.
    unset($user->session);
    unset($user->session_data_present_at_load);

    // Store the session id so we can locate the session with the user id.
    $user->sid = $key;
    dmemcache_set($user->uid, $user, ini_get('session.gc_maxlifetime'), 'users');
  }
  return TRUE;
}