/**
- ---CREATE COOKIE OBJECT---
- $c = new Cookie ();
- ---create/update COOKIE---
- $c->setname (' MyCookie ')//REQUIRED
- ->setvalue ($value, True)//required-1st param = Data string/array, 2nd param = Encrypt (true=yes)
- ->setexpire (' +1 hour ')//Optional-defaults to "0" or browser close
- ->setpath ('/')//Optional-defaults to/
- ->setdomain ('. domain.com ')//Optional-will try to Auto-detect
- ->setsecure (FALSE)//Optional-default False
- ->sethttponly (FALSE); Optional-default false
- $c->createcookie (); Could chain this too, must is last
- ---DESTROY COOKIE---
- $c->setname (' MyCookie ')->destroycookie ();
- OR
- $c->destroycookie (' MyCookie ');
- ---GET COOKIE---
- $cookie = $c->getcookie (' MyCookie ', true); 1st param = cookie name, 2nd param = whether to decrypt
- If the value is a array, you'll need to unserialize the return
*/
- Class Cookie {
- Cookie encryption key value string, can be modified according to your own needs
- Const Des_key = ' o89l7234kjw2wad72shw22lpzmebp3dsj7tt10a5sh60 ';
Private $errors = Array ();
- Private $cookieName = null;
- Private $cookieData = null;
- Private $cookieKey = null;
- Private $cookieExpire = 0;
- Private $cookiePath = '/';
- Private $cookieDomain = null;
- Private $cookieSecure = false;
- Private $cookieHTTPOnly = false;
- /**
- * Constructor Set Domain
- * @access Public
- * @return None
- */
- Public Function __construct ()
- {
- $this->cookiedomain = $this->getrootdomain ();
- }
- /**
- * Get Cookie value
- * @access Public
- * @param string $cookieName cookie to be retrieved
- * @param bool $decrypt Whether to decrypt the values
- */
- Public Function GetCookie ($cookieName =null, $decrypt =false)
- {
- if (Is_null ($cookieName)) {
- $cookieName = $this->cookiename;
- }
- if (Isset ($_cookie[$cookieName])) {
- Return ($decrypt $this->cookieencryption ($_cookie[$cookieName],true): Base64_decode ($_cookie[$cookieName]);
- } else {
- $this->pusherror ($cookieName. ' Not found ');
- return false;
- }
- }
- /**
- * Create cookies
- * @access Public
- * @return BOOL True/false
- */
- Public Function Createcookie ()
- {
- if (Is_null ($this->cookiename)) {
- $this->pusherror (' Cookie name was null ');
- return false;
- }
- $ret = Setcookie (
- $this->cookiename,
- $this->cookiedata,
- $this->cookieexpire,
- $this->cookiepath,
- $this->cookiedomain,
- $this->cookiesecure,
- $this->cookiehttponly
- );
- return $ret;
- }
- /**
- * Clear Cookies
- * @access Public
- * @param string $cookieName to Kill
- * @return BOOL True/false
- */
- Public Function Destroycookie ($cookieName =null)
- {
- if (Is_null ($cookieName)) {
- $cookieName = $this->cookiename;
- }
- $ret = Setcookie (
- $cookieName,
- Null
- (Time ()-1),
- $this->cookiepath,
- $this->cookiedomain
- );
- return $ret;
- }
- /**
- * Set cookie Name
- * @access Public
- * @param string $name cookie Name
- * @return Mixed obj or bool false
- */
- Public Function SetName ($name =null)
- {
- if (!is_null ($name)) {
- $this->cookiename = $name;
- return $this;
- }
- $this->pusherror (' Cookie name was null ');
- return false;
- }
- /**
- * Set Cookie Value
- * @access Public
- * @param string $value cookie value
- * @return BOOL Whether the string was a string
- */
- Public Function SetValue ($value =null, $encrypt =false)
- {
- if (!is_null ($value)) {
- if (Is_array ($value)) {
- $value = serialize ($value);
- }
- $data = ($encrypt? $this->cookieencryption ($value): Base64_encode ($value));
- $len = (function_exists (' Mb_strlen ')? Mb_strlen ($data): strlen ($data));
- if ($len >4096) {
- $this->pusherror (' Cookie data exceeds 4kb ');
- return false;
- }
- $this->cookiedata = $data;
- Unset ($data);
- return $this;
- }
- $this->pusherror (' Cookie value was empty ');
- return false;
- }
- /**
- * Set the expiration time of the cookie
- * @access Public
- * @param string $time +1 week, etc.
- * @return BOOL Whether the string was a string
- */
- Public Function Setexpire ($time =0)
- {
- $pre = substr ($time, 0, 1);
- if (In_array ($pre, Array (' + ', '-'))) {
- $this->cookieexpire = Strtotime ($time);
- return $this;
- } else {
- $this->cookieexpire = 0;
- return $this;
- }
- }
- /**
- * Set the save path of the cookie
- * @access Public
- * @param string $path
- * @return Object $this
- */
- Public Function SetPath ($path = '/')
- {
- $this->cookiepath = $path;
- return $this;
- }
- /**
- * Set the domain to which the cookie belongs
- * @access Public
- * @param string $domain
- * @return Object $this
- */
- Public Function SetDomain ($domain =null)
- {
- if (!is_null ($domain)) {
- $this->cookiedomain = $domain;
- }
- return $this;
- }
- /**
- *
- * @access Public
- * @param bool $secure True/false
- * @return Object $this
- */
- Public Function setsecure ($secure =false)
- {
- $this->cookiesecure = (bool) $secure;
- return $this;
- }
- /**
- * HttpOnly flag, not yet fully supported by all browsers
- * @access Public
- * @param bool $httponly yes/no
- * @return Object $this
- */
- Public Function sethttponly ($httponly =false)
- {
- $this->cookiehttponly = (bool) $httponly;
- return $this;
- }
- /**
- * Jenky bit to retrieve root domain if not supplied
- * @access Private
- * @return String Le Domain
- */
- Private Function Getrootdomain ()
- {
- $host = $_server[' http_host ');
- $parts = Explode ('. ', $host);
- if (count ($parts) >1) {
- $tld = Array_pop ($parts);
- $domain = Array_pop ($parts). '. '. $tld;
- } else {
- $domain = Array_pop ($parts);
- }
- Return '. '. $domain;
- }
- /**
- * Value Encryption
- * @access Private
- * @param string $str string to be (De|en) crypted
- * @param string $decrypt whether to decrypt or not
- * @return String (de|en) crypted string
- */
- Private Function Cookieencryption ($str =null, $decrypt =false)
- {
- if (Is_null ($STR)) {
- $this->pusherror (' cannot encrypt/decrypt null string ');
- return $str;
- }
$iv _size = mcrypt_get_iv_size (Mcrypt_3des, MCRYPT_MODE_ECB);
- $iv = Mcrypt_create_iv ($iv _size, Mcrypt_rand);
- $key _size = mcrypt_get_key_size (Mcrypt_3des, MCRYPT_MODE_ECB);
- $key = substr (self::D es_key,0, $key _size);
if ($decrypt) {
- $return = Mcrypt_decrypt (Mcrypt_3des, $key, Base64_decode ($STR), MCRYPT_MODE_ECB, $IV);
- } else {
- $return = Base64_encode (Mcrypt_encrypt (Mcrypt_3des, $key, $str, MCRYPT_MODE_ECB, $iv));
- }
return $return;
- }
- /**
- * ADD error to errors array
- * @access Public
- * @param string $error
- * @return None
- */
- Private Function Pusherror ($error =null)
- {
- if (!is_null ($error)) {
- $this->errors[] = $error;
- }
- Return
- }
- /**
- * Retrieve Errors
- * @access Public
- * @return String errors
- */
- Public Function geterrors ()
- {
- Return implode ("
", $this->errors);
- }
- }
Copy CodeInvocation Example:
Require (' cookie.class.php ');
Sample data
- $array = Array (' foo ' = ' bar ', ' bar ' = ' foo ');
- $string = ' This is a string ';
$c = new Cookie ();
/*
- Create an array of encrypted cookies
- */
- Echo '
Encrypted Array ';
$start = Microtime (true);
$c->setname (' Example ')//Our cookie Name
- ->setvalue ($array, True)//second parameter, true, encrypts data
- ->setexpire (' +1 hours ')//expires in 1 hour
- ->setpath ('/')//cookie Path
- ->setdomain (' localhost ')//set for localhost
- ->createcookie ();
- $cookie = $c->getcookie (' Example ', true);
- $cookie = Unserialize ($cookie);
$bench = sprintf ('%.8f ', (Microtime (True)-$start));
Echo Print_r ($cookie, true). ' '. $bench. ' Seconds ';
/*
- Destroying cookies
- */
- $c->destroycookie (' Example ');
/*
- Create a cookie string that fails when the browser is closed directly
- */
- Echo '
Regular unencrypted string ';
- $start = Microtime (true);
- $c->setname (' Example1 ')
- ->setvalue ($string)//Second Param could is set to false here
- ->setexpire (0)
- ->setpath ('/')
- ->setdomain (' localhost ')
- ->createcookie ();
$cookie = $c->getcookie (' Example1 ');
$bench = sprintf ('%.8f ', (Microtime (True)-$start));
Echo Print_r ($cookie, true). ' '. $bench. ' Seconds ';
Copy Code |