加入收藏 | 设为首页 | 会员中心 | 我要投稿 惠州站长网 (https://www.0752zz.com.cn/)- 办公协同、云通信、物联设备、操作系统、高性能计算!
当前位置: 首页 > 教程 > 正文

php实现的Cookies操作类实例

发布时间:2022-06-22 09:02:33 所属栏目:教程 来源:互联网
导读:这篇文章主要介绍了php实现的Cookies操作类及其用法实例,包括了常见了保存、读
  这篇文章主要介绍了php实现的Cookies操作类及其用法实例,包括了常见了保存、读取、更新及清除cookie等操作,在需要进行cookie操作时非常具有实用价值,需要的朋友可以参考下。
 
  本文实例讲述了PHP实现的Cookies操作类及其用法,分享给大家供大家参考。具体分析如下:
 
  一、功能:
 
  1.保存,读取,更新,清除cookies数据。
 
  2.可设置前缀。
 
  3.强制超时控制。
 
  4.cookies数据可以是字符串,数组,对象等。
 
  二、用法:
 
  Cookies.class.php类文件如下:
 
  <?php  
  /** Cookies class 保存,读取,更新,清除cookies数据。可设置前缀。强制超时。数据可以是字符串,数组,对象等。  
  *  Date:  2013-12-22  
  *  Author: fdipzone  
  *  Ver:  1.0  
  *  
  *  Func:  
  *  public  set    设置cookie  
  *  public  get    读取cookie  
  *  public  update   更新cookie  
  *  public  clear   清除cookie  
  *  public  setPrefix 设置前缀  
  *  public  setExpire 设置过期时间  
  *  private authcode  加密/解密  
  *  private pack    将数据打包  
  *  private unpack   将数据解包  
  *  private getName  获取cookie name,增加prefix处理  
  */
     
  class Cookies{ // class start  
     
    private $_prefix = '';                         // cookie prefix  
    private $_securekey = 'ekOt4_Ut0f3XE-fJcpBvRFrg506jpcuJeixezgPNyALm';  // encrypt key  
    private $_expire = 3600;                        // default expire  
     
    /** 初始化  
    * @param String $prefix   cookie prefix  
    * @param int  $expire   过期时间  
    * @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;  
      }  
     
    }  
     
    /** 设置cookie  
    * @param String $name  cookie name  
    * @param mixed $value cookie value 可以是字符串,数组,对象等  
    * @param int  $expire 过期时间  
    */
    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);  
      }  
     
    }  
     
    /** 读取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;  
      }  
     
    }  
     
    /** 更新cookie,只更新内容,如需要更新过期时间请使用set方法  
    * @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){ // 获取之前的过期时间  
     
          $cookie_expire = $old_cookie_value[1];  
     
          // 更新数据  
          $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;  

(编辑:惠州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读