You are here

function session_cache_set in Session Cache API 7

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

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

Parameters

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

mixed $data: A number or string, an object, a multi-dimensional array etc. $bin is the identifier you choose for the data you want to store. To guarante uniqueness, you could prefix it with the name of the module that you use this API with. Use NULL to erase the bin; it may be recreated and refilled at any time by calling the function with a non-NULL data argument.

File

./session_cache.module, line 42
session_cache.module

Code

function session_cache_set($bin, $data) {
  if (!isset($bin)) {
    return;
  }
  $method = variable_get('session_cache_storage_method', SESSION_CACHE_STORAGE_SESSION);
  switch ($method) {
    case SESSION_CACHE_STORAGE_COOKIE:
      $serialized_data = $data == NULL ? NULL : json_encode($data);

      // For $cookie_domain see also: drupal_settings_initialize()
      global $cookie_domain;

      // .localhost causes problems
      $cdom = $cookie_domain == '.localhost' ? ini_get('session.cookie_domain') : $cookie_domain;

      // Tell the browser to return the cookie only via HTTP or HTTPS, to any
      // path under the root of this server.
      setcookie("Drupal.session_cache.{$bin}", $serialized_data, $data == NULL ? 0 : session_cache_expiration_time(), '/', $cdom, 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_clear_all("{$bin}:{$sid}", 'cache_session_cache');
      }
      else {
        cache_set("{$bin}:{$sid}", $data, 'cache_session_cache', session_cache_expiration_time());
      }
      return;
    case SESSION_CACHE_STORAGE_SESSION:

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