You are here

function sess_write in Memcache API and Integration 5

Same name and namespace in other branches
  1. 5.2 session-memcache.inc \sess_write()
  2. 5.2 session-memcache-db.inc \sess_write()
  3. 5.2 session-memcache.db.inc \sess_write()
  4. 6 memcache-session.inc \sess_write()

File

./memcache-session.inc, line 49
User session handling functions.

Code

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 session saving has been turned off, do nothing.
  if (empty($_COOKIE[session_name()]) && empty($value) || !session_save_session()) {
    return TRUE;
  }

  // Prepare the information to be saved
  $session = new stdClass();
  $session->sid = $key;
  $session->uid = $user->uid;
  $session->cache = $user->cache;
  $session->hostname = $_SERVER["REMOTE_ADDR"];
  $session->session = $value;
  $session->timestamp = $_SERVER['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 = sess_user_load($session);
  if ($user->uid || $value) {
    dmemcache_set($key, $session, ini_get('session.gc_maxlifetime'), 'session');
    dmemcache_set($user->uid, $user, ini_get('session.gc_maxlifetime'), 'users');
    if ($user->uid && $user->access < $_SERVER['REQUEST_TIME'] - 300) {
      db_query("UPDATE {users} SET access = %d WHERE uid = %d", $_SERVER['REQUEST_TIME'], $user->uid);
    }
  }
  return TRUE;
}