PHP implementation of the cookie operation class instance, Phpcookies class instance
This article describes the PHP implementation of the cookie operation class and its usage, share to everyone for your reference. The specific analysis is as follows:
First, the function:
1. Save, read, update, erase cookies data.
2. Prefix can be set.
3. Force timeout control.
4.cookies data can be strings, arrays, objects, and so on.
Second, usage:
The Cookies.class.php class file is as follows:
<?php/** Cookies class to save, read, update, and erase cookies data. Prefix can be set. Force timeout. The data can be strings, arrays, objects, and so on. * date:2013-12-22 * Author:fdipzone * ver:1.0 * * Func: * Public Set setting cookie * Public get Read Cookie * Public Update Cookies * Public clear clear cookies * Public Setprefix set prefix * Public setexpire set Expiration time * Private AUT Hcode Encryption/decryption * Private Pack package data * Private Unpack unpack the data * Private GetName Get cookie name, add prefix processing */class Coo kies{//class start private $_prefix = '; Cookie prefix private $_securekey = ' Ekot4_ut0f3xe-fjcpbvrfrg506jpcujeixezgpnyalm '; Encrypt key Private $_expire = 3600; Default expire/** initialization * @param string $prefix cookie prefix * @param int $expire Expiry time * @param string $s Ecurekey Cookie Secure Key */Public function __construct ($prefix = ", $expire =0, $securekey =") {if (Is_string ($PR Efix) && $prefix! = ") {$this->_prefix = $prefix; } if (Is_numerIC ($expire) && $expire >0) {$this->_expire = $expire; } if (Is_string ($securekey) && $securekey! = ") {$this->_securekey = $securekey; }}/** Set cookie * @param string $name cookie name * @param mixed $value cookie value can be a string, array, object, etc. * @param int $expire Expiration Time */Public function set ($name, $value, $expire =0) {$cookie _name = $this->getname ($name); $cookie _expire = time () + ($expire? $expire: $this->_expire); $cookie _value = $this->pack ($value, $cookie _expire); $cookie _value = $this->authcode ($cookie _value, ' ENCODE ', $this->_securekey); if ($cookie _name && $cookie _value && $cookie _expire) {Setcookie ($cookie _name, $cookie _value, $cookie _ expire); }}/** Read cookie * @param String $name Cookie name * @return Mixed Cookie value */Public function get ( $name) {$cookie _name = $this->getname ($name); if (Isset ($_cookie[$cookie _name])) { $cookie _value = $this->authcode ($_cookie[$cookie _name], ' DECODE ', $this->_securekey); $cookie _value = $this->unpack ($cookie _value); return Isset ($cookie _value[0])? $cookie _value[0]: null; }else{return null; }}/** update the cookie, update the content only, if you need to update the expiration time, use the Set method * @param String $name Cookie name * @param mixed $value cookie value * @return Boolean */Public Function update ($name, $value) {$cookie _name = $this->getname ($name); if (Isset ($_cookie[$cookie _name])) {$old _cookie_value = $this->authcode ($_cookie[$cookie _name], ' DECODE ', $this-& Gt;_securekey); $old _cookie_value = $this->unpack ($old _cookie_value); if (Isset ($old _cookie_value[1]) && $old _cookie_vlaue[1]>0) {//Get previous expiration time $cookie _expire = $old _cookie_v ALUE[1]; Update data $cookie _value = $this->pack ($value, $cookie _expire); $cookie _value = $this->authcode ($cookie _value, ' ENCODE ', $this->_securekey); if ($cookie _name && $cookie _value && $cookie _expire) {Setcookie ($cookie _name, $cookie _value , $cookie _expire); return true; }}} return false; /** Clear Cookie * @param String $name Cookie name */Public Function Clear ($name) {$cookie _name = $this GetName ($name); Setcookie ($cookie _name); }/** Set prefix * @param String $prefix Cookie Prefix */public Function Setprefix ($prefix) {if (Is_string ($prefix && $prefix! = ") {$this->_prefix = $prefix; }}/** set expiration time * @param int $expire Cookie expire */Public Function Setexpire ($expire) {if (Is_numeric ($ex Pire) && $expire >0) {$this->_expire = $expire; }}/** Gets the cookie name * @param string $name * @return String */Private Function GetName ($name) {return $ This->_prefix? $this->_prefix. ' _ '. $name: $name; }/** Pack * @param Mixed $data data * @param int $EXPIThe RE expiration time is used to determine * @return */Private Function Pack ($data, $expire) {if ($data = = =) {return '; } $cookie _data = Array (); $cookie _data[' value '] = $data; $cookie _data[' expire ') = $expire; Return Json_encode ($cookie _data); }/** Unpack * @param Mixed $data data * @return Array (data, expiry time) */Private Function unpack ($data) {if ( $data = = = ") {return array (', 0); } $cookie _data = Json_decode ($data, true); if (isset ($cookie _data[' value ')) && isset ($cookie _data[' expire ')) {if (Time () < $cookie _data[' expire ']) {/ /unexpired return Array ($cookie _data[' value '), $cookie _data[' expire ']); }} return Array ("', 0); /** Encrypt/Decrypt data * @param string $str original text or ciphertext * @param string $operation ENCODE or DECODE * @return string according to the set Authcode ($string, $operation = ' DECODE ') {$ckey _length = 4; Random key length value 0-32; $key = $this->_securekey; $key =MD5 ($key); $keya = MD5 (substr ($key, 0, 16)); $KEYB = MD5 (substr ($key, 16, 16)); $KEYC = $ckey _length? ($operation = = ' DECODE '? substr ($string, 0, $ckey _length): substr (MD5 (Microtime ()),-$ckey _length)): "; $cryptkey = $keya. MD5 ($keya. $KEYC); $key _length = strlen ($cryptkey); $string = $operation = = = ' DECODE '? Base64_decode (substr ($string, $ckey _length)): sprintf ('%010d ', 0). substr (MD5 ($string. $keyb), 0, +). $string; $string _length = strlen ($string); $result = "; $box = Range (0, 255); $rndkey = Array (); for ($i = 0; $i <= 255; $i + +) {$rndkey [$i] = Ord ($cryptkey [$i% $key _length]); } for ($j = $i = 0; $i <, $i + +) {$j = ($j + $box [$i] + $rndkey [$i])% 256; $tmp = $box [$i]; $box [$i] = $box [$j]; $box [$j] = $tmp; } for ($a = $j = $i = 0; $i < $string _length; $i + +) {$a = ($a + 1)% 256; $j = ($j + $box [$a])% 256; $tmp = $box [$a]; $box [$a] = $box [$j];$box [$j] = $tmp; $result. = Chr (ord ($string [$i]) ^ ($box [($box [$a] + $box [$j])% 256])); if ($operation = = ' DECODE ') {if (substr ($result, 0, ten) = = 0 | | substr ($result, 0,)-time () > 0) & & Substr ($result, ten, +) = = substr (MD5 ($result, $keyb), 0, +) {return substr ($result, 26); } else {return '; }} else {return $KEYC. Str_replace (' = ', ' ', Base64_encode ($result)); }}}//class end?>
The
demo.php sample program is as follows:
<?php require ' Cookies.class.php '; $type = isset ($_get[' type ')? Strtolower ($_get[' type '): '; if (!in_array ($type, Array (' Set ', ' Get ', ' Update ', ' Clear '))) {exit (' type NOT EXISTS ')} $obj = new Cookie (' member ', 10); obj switch ($type) {case ' Set '://Set $data = Array (' name ' = ' fdipzone ', ' gender ' = ' male ') ); $obj->set (' Me ', $data, 5); echo ' Set cookies '; Break Case ' get '://Read $result = $obj->get (' Me '); Echo ' '; echo ' Get cookies '; Break Case ' Update '://update $data = Array (' name ' = ' Angelababy ', ' gender ' = ' female '); $flag = $obj->update (' Me ', $data); if ($flag) {echo ' Update cookie success '; }else{echo ' update cookie false '; } break; Case ' clear '://Clear $obj->clear (' Me '); echo ' Clear cookies '; Break }?>
This article full example source click here to download this site.
I hope this article is helpful to everyone's PHP programming.
How do cookies in PHP set multiple values as ASP can set multiple values
Set the cookie
Setcookie ("Cookie[three]", "Cookiethree");
Setcookie ("Cookie[two]", "cookietwo");
Setcookie ("Cookie[one]", "Cookieone");
After the page reloads, print them out
if (Isset ($_cookie[' COOKIE ")) {
foreach ($_cookie[' COOKIE '] as $name = = $value) {
echo "$name: $value \ n";
}
}
?>
Examples in the manual
How PHP reads Cookies
"IT168 Technical documentation" must be assigned to Cookies before the server transmits any content to the customer's browser. To do this, the cookies must be set in the < HEAD tag:< Phpsetcookie ("Cookieid", $USERID); < html< body</body cookie, is a string, for example: "Cookieid". colons, commas, and spaces are not allowed during this period. This parameter is required, and all other parameters are optional. If only this parameter is given, then the cookie will be deleted. The value of the
cookie, usually a string variable, for example: $USERID. Can I assign one to it? To skip the value setting.
The time the cookie expires. If omitted (or assigned a value of 0), the cookie will expire at the end of the conversation session. This parameter can be an absolute time, expressed in Dd-mon-yy HH:MM:SS, for example: "24-nov-99 08:26:00". The more common is to set a relative time. This is done through the time () function or the Mktime function. For example, time () +3600 will invalidate the cookie after one hours.
A path to match the cookie. This parameter is used to avoid confusion when there is more than one set of cookies with the same name on a server. The effect of using the "/" path and omitting this parameter is the same. Note that Netscape's cookie definition is to place the domain name in front of the path, while PHP is the opposite. The domain name of the
server is also used to match cookies. Note that you must place a dot before the server's domain name (. )。 For example: ". friendshipcenter.com". Because unless there are more than two points in existence, this parameter is not acceptable. The security level of the
cookie is an integer. 1 means that the cookie can only be transmitted through a "secure" network. 0 or omitted means that any type of network can be
http://www.bkjia.com/PHPjc/883685.html www.bkjia.com true http://www.bkjia.com/PHPjc/883685.html techarticle PHP Implementation of the cookie operation class example, Phpcookies class example of this article describes the PHP implementation of the cookie operation class and its usage, share for everyone for reference. The specific analysis is as follows: one 、...