码迷,mamicode.com
首页 > Web开发 > 详细

PHP会话cookie类的封装

时间:2018-07-25 13:53:46      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:读取   fun   选项   val   else   etop   sse   func   封装   

<?php
header(‘content-type:text/html;charset=utf-8‘);
/**
 * 完成cookie的设置、删除、更新、读取
 */
class Cookie{
    private static $instance=null;
    private  $expire=0;//过期时间 单位为s 默认是会话 关闭浏览器就不在存在
    private $path=‘‘;//路径 默认在本目录及子目录下有效 /表示根目录下有效
    private $domain=‘‘;//域
    private $secure=false;//是否只在https协议下设置默认不是
    private $httponly=false;//如果为TRUE,则只能通过HTTP协议访问cookie。 这意味着脚本语言(例如JavaScript)无法访问cookie
    /**
     * [__construct description]
     * 构造函数完成cookie参数初始化工作
     * @DateTime 2018-07-25T09:50:51+0800
     * @param    array                    $options [cookie相关选项]
     */
    private function __construct(array $options=[]){
        $this->getOptions($options);        
    }
    private function getOptions(array $options=[]){
        if(isset($options[‘expire‘])) {$this->expire=$options[‘expire‘];}    
        if(isset($options[‘path‘])) $this->path=$options[‘path‘];
        if(isset($options[‘domain‘])) $this->domain=$options[‘domain‘];
        if(isset($options[‘secure‘])) $this->secure=$options[‘secure‘];
        if(isset($options[‘httponly‘])) $this->httponly=$options[‘httponly‘];    
        return $this;
    }
    /**
     * [getInstance description]
     * 单例模式
     * @DateTime 2018-07-25T09:50:01+0800
     * @param    array                    $options [cookie相关选项]
     * @return   object                    $options [对象实例]
     */
    public static function getInstance(array $options=[]){
        if(is_null(self::$instance)){
            $class=__CLASS__;
            self::$instance=new $class($options);
        }
        return self::$instance;
    }
    /**
     * 设置cookie
     * Func description
     * @DateTime 2018-07-25T09:42:37+0800
     * @param    [string]                 $name    [cookie名称]
     * @param    [mixed]                   $vlaue   [cookie值]
     * @param    array                    $options [cookie相关选项]
     */
    public function set($name,$value,array $options=[]){
        if(is_array($options)&&count($options)>0){
            $this->getOptions($options);
        }
        if(is_array($value)||is_object($value)){
            $value=json_encode($value);
            var_dump($value);
            
        }
         setcookie($name,$value,$this->expire,$this->path,$this->domain,$this->secure,$this->httponly);
    }
    /**
     * [get description]
     * 读取cookie值
     * @DateTime 2018-07-25T11:34:04+0800
     * @param    [string]                   $name [cookie名称]
     * @return   [mixed]                         [数组形式的值或者单个的值]
     */
    public function get($name){
        $value=$_COOKIE[$name];
        if(is_array($value)){
            $arr=[];
            foreach ($value as $k => $v) {
                # code...
                $arr[$k]=substr($v, 0,1)==‘{‘? json_decode($value):$v;
            }
            return $arr;
        }else{
        return substr($value, 0,1)==‘{‘? json_decode($value):$value;}
    }
    /**
     * [delete description]
     * 删除相应的cookie
     * @DateTime 2018-07-25T11:53:24+0800
     * @param    [string]                   $name    [cookie名称 可以是数组]
     * @param    array                    $options [cookie相关参数]
     * @return   [type]                            [description]
     */
    public function delete($name,array $options=[]){
        if(is_array($options)&&count($options)>0){
            $this->getOptions($options);
        }
        $value=$_COOKIE[$name];        
        if($value){
            if(is_array($value)){
    
                foreach ($value as $k => $v) {
                    # code...
                     setcookie($name.‘[‘.$k.‘]‘,‘‘,time()-1,$this->path,$this->domain,$this->secure,$this->httponly);
                    unset($v);
                }                
            }else{
            setcookie($name,‘‘,time()-1,$this->path,$this->domain,$this->secure,$this->httponly);
            unset($value);
        }
        }
    }

}
$cookie=Cookie::getInstance();
//$cookie->set(‘aaaa‘,‘bb‘);
//$cookie->set(‘bb‘,‘bb‘,[‘expire‘=>time()+3600,‘path‘=>‘/‘,‘domain‘=>‘localhost‘,‘secure‘=>false,‘httponly‘=>true]);
//$cookie->set(‘admin‘,[‘name‘=>‘张三‘,‘age‘=>20]);
//$cookie->set(‘user[name]‘,‘李四‘);
//$cookie->set(‘user[age]‘,25);
//var_dump($_COOKIE[‘user‘]);
$cookie->delete(‘user‘);


如有不全面的地方,欢迎大家指正。

PHP会话cookie类的封装

标签:读取   fun   选项   val   else   etop   sse   func   封装   

原文地址:https://www.cnblogs.com/zaoa/p/9365431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!