PHP Cookies Operation class

Source: Internet
Author: User
Tags arrays md5 mixed ord pack set cookie strlen unpack

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 cookies * public    Get read cookie * Public Update update cookies * Public Clear Erase Cookie * 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 ', $this->_securekey); if ($cookiE_name && $cookie _value && $cookie _expire) {Setcookie ($cookie _name, $cookie _value, $cookie _e  
        Xpire);          /** 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 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->getname ($name); if (Isset ($_cookie[$cookie _name])) {$old _cookie_value = $this->authcode ($_cookie[$cookie _name], ' DE  
            CODE ', $this->_securekey);  
      
            $old _cookie_value = $this->unpack ($old _cookie_value);  if (Isset ($old _cookie_value[1]) && $old _cookie_vlaue[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 ', $this->_securekey); if ($cookie _name && $cookie _value && $cookie _expire) {Setcookie ($cookie _name, $cookie  
                    _value, $cookie _expire);  
                return true;  
      
    }} return false; 
    /** Clear Cookies* @param String $name Cookie name */Public Function Clear ($name) {$cookie _name = $this->g  
        Etname ($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 * @param int $expire Cookie Expire * * Public function Setexp   
        IRE ($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 to see more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/php/* @param int $expire Expiration Time to judge * @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)/PRI  
        vate 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 by setting/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, 10)- Time () > 0) && substr ($result) = = substr (MD5 (substr ($result,). $keyb), 0) {RE  
            Turn substr ($result, 26);  
         } else {       Return ";  
        } else {return $KEYC. Str_replace (' = ', ', Base64_encode ($result)); }}//class end?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.