Cookies Operation class
function:
1. Save, read, update, and erase cookies data.
2. You can set a prefix.
3. Force timeout control.
4.cookies data can be strings, arrays, objects, and so on.
Cookies.class.php
<?php/** Cookies class to save, read, update, and erase cookies data. Prefix can be set. Force timeout.
Data can be strings, arrays, objects, and so on. * date:2013-12-22 * Author:fdipzone * ver:1.0 * * * FUNC: * Public Set Set cookies * Public get Read cookies * Public Update update cookies * Public Clear erase cookies * Public Setprefix set prefix * Public Setexpire Set Expiration * Private Authcode encryption/decryption * Private Pack to package the data * Private Unpack unpack the data * private GetName Get cookie name, add prefix Processing/class cookies{//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 expiration * param String $securekey Cookie secure key/Public function __construct ($prefix = ', $expire = 0, $securekey = ') {if (is_string ($prefix) && $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 '); if ($cookie _name && $cookie _value && $cookie _expire) {Setcookie ($cookie _name, $cookie _value, $c
Ookie_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 ');
$cookie _value = $this->unpack ($cookie _value); return Isset ($cookie _value[0])?
$cookie _value[0]: null;
}else{return null; /** update cookies, update only content, if you need to update 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->get
Name ($name);
if (Isset ($_cookie[$cookie _name])) {$old _cookie_value = $this->authcode ($_cookie[$cookie _name], ' DECODE ');
$old _cookie_value = $this->unpack ($old _cookie_value); if (Isset ($old _cookie_value[1]) && $old _cookie_value[1]>0) {//Get the expiration time before $cookie _expire = $old _cookie_value[1];
Update data $cookie _value = $this->pack ($value, $cookie _expire);
$cookie _value = $this->authcode ($cookie _value, ' ENCODE '); if ($cookie _name && $cookie _value && $cookie _expire) {Setcookie ($cookie _name, $cookie _v
Alue, $cookie _expire);
return true;
}} return false; /** the cookie * @param String $name cookie name/Public function Clear ($name) {$cookie _na
me = $this->getname ($name);
Setcookie ($cookie _name); /** Set prefix * @param String $prefix Cookie Prefix */public Function Setprefix ($prefix) {if (is_s
Tring ($prefix) && $prefix!= ') {$this->_prefix = $prefix;
/** Set expiration * @param int $expire cookie expire */Public Function Setexpire ($expire) {if (Is_numeric ($expire) && $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 $expire Expiration Time to judge * @return/PRI
vate 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, expiration)/Private function UNP
ACK ($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 ']) {//not passed
Period 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 St Ring to return plaintext according to set/Private function Authcode ($string, $operation = ' DECODE ') {$ckey _length = 4;
With the secret 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 < 256 $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,) = 0 | | | substr ($result, 0,)-time () ; 0 && substr ($result) = = substr (MD5 (substr ($result). $keyb) (0)) {return substr ($result, 26);
else {return ';
} else {return $KEYC. Str_replace (' = ', ', Base64_encode ($result)); }}//class end?>
demo.php
<?php require ' Cookies.class.php '; $type = isset ($_get[' type ')?
Strtolower ($_get[' type ']): ';
if ($type, Array (' Set ', ' Get ', ' Update ', ' Clear ')) {exit (' type exists ');} $obj = new Cookies (' member ', 10); obj switch ($type) {case ' Set '://Set $data = Array (' name ' => ' fdipzone ', ' Gen
Der ' => ' Male ');
$obj->set (' Me ', $data, 5);
echo ' Set cookies ';
Break
Case ' get '://Read $result = $obj->get (' Me ');
Echo ' <pre> ';
Print_r ($result);
Echo ' </pre> ';
echo ' Get cookies ';
Break
Case ' Update '://update $data = Array (' name ' => ' Angelababy ', ' gender ' => ' female ')
);
$flag = $obj->update (' Me ', $data);
if ($flag) {echo ' Update cookies success ';
}else{echo ' update cookie false ';
} break;
Case ' clear '://Erase $obj->clear (' Me ');
echo ' Clear cookies ';
Break }?>
Source Download Address: Click to view