You are here

function secure_cookie_data_put in Secure Cookie Data 7

Same name and namespace in other branches
  1. 7.2 secure_cookie_data.module \secure_cookie_data_put()

Sets the secure cookie with the given data.

Parameters

string $data: The JSON data to be stored in the cookie.

boolean $raw: If TRUE the cookie is set so that the encryption is done in binary and then base 64 encoded.

Return value

boolean TRUE if the cookie was modified/set, FALSE if not.

File

./secure_cookie_data.module, line 24
secure_data_cookie.module @author António P. P. Almeida <appa@perusio.net> @date Thu Nov 21 11:45:23 2013

Code

function secure_cookie_data_put($data) {

  // Decode the given data.
  $cookie_data = json_decode($data);

  // Compute the HMAC right away.
  $cookie_data->hmac = secureCookieBasic::create($cookie_data);

  // Now we modify or set the cookie if it doesn't exist.
  if (isset($_COOKIE[secureCookieBasic::$__cookie_name])) {
    $_COOKIE[secureCookieBasic::$__cookie_name] = secureCookieBasic::encode($cookie_data);
    return TRUE;
  }
  else {

    // Set the cookie. It expires upon closing of the browser.
    return setcookie(secureCookieBasic::$__cookie_name, secureCookieBasic::encode($cookie_data), 0, secureCookieBasic::$__cookie_path, secureCookieBasic::get_domain(), secureCookieBasic::secure_session_p(), TRUE);
  }
}