You are here

function secure_cookie_data_get in Secure Cookie Data 7

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

Obtain the data stored in a secure cookie.

Parameters

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

Return value

object The stored data or NULL if the cookie is missing or the HMAC doesn't validate.

File

./secure_cookie_data.module, line 59
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_get() {

  // First thing we do is to validate the cookie.
  if (isset($_COOKIE[secureCookieBasic::$__cookie_name])) {

    // Decode the JSON.
    $cookie_data = secureCookieBasic::decode($_COOKIE[secureCookieBasic::$__cookie_name]);

    // Next we get the HMAC.
    if (isset($cookie_data->hmac)) {
      $hmac = $cookie_data->hmac;

      // Unset the hmac entry so that we get the 'raw' data only.
      unset($cookie_data->hmac);

      // Validate the cookie using the HMAC stored on the data.
      if (secureCookieBasic::validate($hmac, $cookie_data)) {

        // Return the data.
        return $cookie_data;
      }
    }
  }
  return NULL;
}