Skip to content

Version 2.1

Salvo Quaranta (Zeroastro) edited this page Feb 1, 2019 · 1 revision

Cookie

To initialise a Cookie object you can use the new statement, or you can create it with a JSON using the createFromJSON static function

$cookieWithNew = new \Sqz\CookieHandler\Cookie( 
    string NAME , 
    string VALUE [, 
    int EXPIRATION, 
    string PATH, 
    string DOMAIN, 
    bool HTTPS_ONLY, 
    bool HTTP_ONLY ] 
);


$cookieWithJson = \Sqz\CookieHandler\Cookie::createFromJSON({ 
    "name":"NAME", 
    "value":"VALUE", 
    "expire":"EXPIRATION", 
    "path":"PATH", 
    "domain":"DOMAIN", 
    "secure":"HTTPS_ONLY",
    "httpOnly":"HTTP_ONLY"
});

Only Name and Value are Mandatory. Default values for the other fields are:

  • Expiration: 0 (session)
  • Path: /
  • Domain: ''
  • Secure: false
  • HttpOnly: true

The Cookie class provides getters and setters for each field.

CookieHandler

You can initialize the Cookie Handler with encryption enabled or disabled. To enable the encryption, you need to pass into the constructor an instance of an object implementing Sqz\CookieHandler\Contracts\CryptographerInterface. A simple cryptographer, Sqz\CookieHandler\CookieCryptographer, which uses OpenSSL, is provided with the package.

$cookieHandler = new \Sqz\CookieHandler\CookieHandler(); // Encryption disabled

$cryptographer = new \Sqz\CookieHandler\CookieCryptographer(string ENCRYPTION_KEY); // Instance of CryptographerInterface
$cookieHandlerSecure = new \Sqz\CookieHandler\CookieHandler($cryptographer); // Encryption enabled

Once the class has ben initialized, you can Save, Retrieve and Remove cookies. Saving a cookie using the name of an existing cookie will overwrite it.

$cookieHandler->saveCookie(\Sqz\CookieHandler\Cookie COOKIE);
$cookie = $cookieHandler->getCookie(string COOKIE_NAME);
$cookieHandler->removeCookie(string COOKIE_NAME);
Clone this wiki locally