/**
- --- Create cookie object ---
- $ C = new cookie ();
-
- --- CREATE/update cookie ---
- $ C-> setName ('mycooker') // 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/
- -> SetDomain ('.domain.com') // optional-will try to auto-detect
- -> SetSecure (false) // optional-default false
- -> SetHTTPOnly (false); // optional-default false
- $ C-> createCookie (); // you cocould chain this too, must be last
-
- --- Destroy cookie ---
- $ C-> setName ('mycooker')-> destroyCookie ();
- OR
- $ C-> destroyCookie ('mycooker ');
-
- --- Get cookie ---
- $ Cookie = $ c-> getCookie ('mycooker', true); // 1st param = cookie name, 2nd param = whether to decrypt
- // If the value is an array, you'll need to unserialize the return
*/
- Class Cookie {
- // Cookie encryption key value string, which can be modified as needed
- 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 setting domain
- * @ Access public
- * @ Return none
- */
- Public function _ construct ()
- {
- $ This-> cookieDomain = $ this-> getRootDomain ();
- }
-
- /**
- * Obtain the 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 a cookie
- * @ 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 the 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 the 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 & gt; 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 cookie expiration time.
- * @ 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 cookie storage path
- * @ 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 ('could not 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: DES_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 );
- }
- }
Call example:
Require ('cookie. class. php ');
// Sample data
- $ Array = array ('foo' => 'bar', 'bar' => 'Foo ');
- $ String = 'This is a string ';
$ C = new Cookie ();
/*
- Create an encrypted cookie array
- */
- Echo 'encryption 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 );
$ Duration = sprintf ('%. 8f', (microtime (true)-$ start ));
Echo print_r ($ cookie, true ).' '. $ Seconds.' seconds ';
/*
- Destroy cookie
- */
- // $ C-> destroyCookie ('example ');
/*
- Create a cookie string, which becomes invalid when the browser is closed.
- */
- Echo 'regular unencrypted string ';
- $ Start = microtime (true );
- $ C-> setName ('example1 ')
- -> SetValue ($ string) // Second param cocould be set to false here
- -> SetExpire (0)
- -> SetPath ('/')
- -> SetDomain ('localhost ')
- -> CreateCookie ();
$ Cookie = $ c-> getCookie ('example1 ');
$ Duration = sprintf ('%. 8f', (microtime (true)-$ start ));
Echo print_r ($ cookie, true ).' '. $ Queue. 'seconds ';
|