You are here

function session_cache_set in Session Cache API 8

Same name and namespace in other branches
  1. 6 session_cache.module \session_cache_set()
  2. 7 session_cache.api.php \session_cache_set()
  3. 7 session_cache.module \session_cache_set()

Write the supplied data to the user session, whatever the storage mechanism may be.

Parameters

string $bin, unique id, eg a string prefixed by the module name:

$data, use NULL to delete the bin; it may be refilled at any time:

File

./session_cache.module, line 27
session_cache.module

Code

function session_cache_set($bin, $data) {
  if (!isset($bin)) {
    return;
  }
  $method = config('session_cache.settings')
    ->get('storage_method') ?: SESSION_CACHE_STORAGE_SESSION;
  switch ($method) {
    case SESSION_CACHE_STORAGE_COOKIE:

      // Tell the browser to return the cookie only via HTTP or HTTPS, to any
      // path under the root of this server.
      // Don't use "global $cookie_domain", as it may contain the invalid '.localhost'!
      $cookie_domain = ini_get('session.cookie_domain');
      $serialized_data = $data == NULL ? NULL : serialize($data);
      setcookie("Drupal.session_cache.{$bin}", $serialized_data, $data == NULL ? 0 : session_cache_expiration_time(), '/', $cookie_domain, NULL, TRUE);

      // Make sure that subsequent session_cache_get() calls get the data
      // updated here, not what was in the cookie at the start of the request!
      // Note that in array indices '.' are converted to '_'
      $_COOKIE["Drupal_session_cache_{$bin}"] = $serialized_data;
      return;
    case SESSION_CACHE_STORAGE_DB_CORE:
      $sid = session_cache_get_sid();
      if ($data == NULL) {
        cache('session_cache')
          ->delete("{$bin}:{$sid}");
      }
      else {
        cache('session_cache')
          ->set("{$bin}:{$sid}", $data, session_cache_expiration_time());
      }
      return;
    case SESSION_CACHE_STORAGE_SESSION:
      $_SESSION[$bin] = $data;

      // $data==NULL means unset()
      return;
    default:
      module_invoke_all('session_cache_set', $method, $bin, $data);
  }
}