- /** 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
- * edit:bbs.it-home.org
- * Func:
- * Public Set Cookie
- * Public Get Read Cookie
- * Public Update Update Cookie
- * Public Clear Clear cookies
- * Public setprefix setting prefix
- * Public Setexpire Set Expiration time
- * Private Authcode encryption/decryption
- * Private Pack to package data
- * Private Unpack data unpacking
- * Private GetName Get cookie name, increase 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 Time
- * @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;
- }
- }
- /** Setting cookies
- * @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);
- }
- }
- /** reading Cookies
- * @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 cookies to update content only, use the Set method if you need to update the expiration time
- * @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->_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_value[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;
- }
- /** Clearing Cookies
- * @param String $name cookie Name
- */
- Public function Clear ($name) {
- $cookie _name = $this->getname ($name);
- Setcookie ($cookie _name);
- }
- /** setting prefixes
- * @param String $prefix Cookie Prefix
- */
- Public Function Setprefix ($prefix) {
- if (is_string ($prefix) && $prefix! = ") {
- $this->_prefix = $prefix;
- }
- }
- /** Setting the Expiration time
- * @param int $expire cookie expire
- */
- Public Function Setexpire ($expire) {
- if (Is_numeric ($expire) && $expire >0) {
- $this->_expire = $expire;
- }
- }
- /** Get 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 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, expiration 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 ')} {//Not expired
- 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 returns plaintext ciphertext based on settings
- */
- Private Function 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 (MD 5 (substr ($result, +). $keyb), 0, 16)) {
- Return substr ($result, 26);
- } else {
- Return ';
- }
- } else {
- Return $KEYC. Str_replace (' = ', ' ', Base64_encode ($result));
- }
- }
- }//Class end
- ?>
Copy Code2, demo example demo.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 '
'; - Print_r ($result);
- 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
- }
- ?>
Copy CodeAttached, the source code download address of the PHP cookie operation class |