You are here

function sess_write in Memcache API and Integration 5.2

Same name in this branch
  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()
Same name and namespace in other branches
  1. 5 memcache-session.inc \sess_write()
  2. 6 memcache-session.inc \sess_write()

File

./session-memcache.inc, line 56
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 (empty($_COOKIE[session_name()]) && empty($value)) {
    return TRUE;
  }

  //  $result = db_query("SELECT sid FROM {sessions} WHERE sid = '%s'", $key);
  $result = dmemcache_get($key, 'session');

  // Prepare the information to be saved
  $info = new stdClass();
  $info->sid = $key;
  $info->uid = $user->uid;
  $info->cache = $user->cache;
  $info->hostname = $_SERVER["REMOTE_ADDR"];
  $info->session = $value;
  $info->timestamp = time();
  if (!$result) {

    // Only save session data when when the browser sends a cookie. This keeps
    // crawlers out of session table. This improves speed up queries, reduces
    // memory, and gives more useful statistics. We can't eliminate anonymous
    // session table rows without breaking throttle module and "Who's Online"
    // block.
    if ($user->uid || $value || count($_COOKIE)) {
      dmemcache_set($key, $info, ini_get('session.gc_maxlifetime'), 'session');
    }
  }
  else {
    dmemcache_set($key, $info, ini_get('session.gc_maxlifetime'), 'session');
    if ($user->uid) {
      dmemcache_set($user->uid, $info->timestamp, 0, 'user_access');
    }
  }
  return TRUE;
}