PHP會話cookie類的封裝
<?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類的封裝