IT源码网

phpcookie类讲解

lxf 2021年02月16日 编程语言 142 0

1.需求

写一个cookie类

2.例子

<?php 
 
class cookie{ 
    private $COOKIE_PREFIX = ""; 
    private $COOKIE_EXPIRE = ""; 
    private $COOKIE_PATH = ""; 
    private $COOKIE_DOMAIN = ""; 
 
    public function __construct() 
    { 
        //这里要加载config并赋值cookie的相关参数 
    } 
 
    public function set_prefix($prefix='') 
    { 
        $this->COOKIE_PREFIX=$prefix; 
    } 
 
    public function get_prefix() 
    { 
        return $this->COOKIE_PREFIX; 
    } 
 
    public function is_set_cookies($name) 
    { 
        return isset($_COOKIE[$this->COOKIE_PREFIX . $name]); 
    } 
 
    public function get_cookie($name) 
    { 
        //正式环境下,这里要解密cookie 
        $value = $_COOKIE[$this->COOKIE_PREFIX . $name]; 
        return $value; 
    } 
 
    public function set_cookie($name, $value, $expire = "", $PATH = "", $domain = "", $prefix = "") 
    { 
        if (empty($expire)) 
        { 
            $expire = $this->COOKIE_EXPIRE; 
        } 
        else 
        { 
            $expire = time()+$expire; 
        } 
        if (empty($path)) { 
            $path = $this->COOKIE_PATH; 
        } 
 
        if (empty($domain)) { 
            $domain = $this->COOKIE_DOMAIN; 
        } 
 
        if (empty($prefix)) { 
            $prefix = $this->COOKIE_PREFIX; 
        } 
        //value要加密 
        setcookie($prefix . $name, $value, $expire, $path, $domain, NULL, true); 
        $_COOKIE[$prefix . $name] = $value; 
    } 
 
    public function delete_cookie($name) 
    { 
        setcookie($name,'',-1); 
        $_COOKIE[$this->COOKIE_PREFIX . $name]=null; 
        unset($_COOKIE[$this->COOKIE_PREFIX . $name]); 
    } 
    public function clear_all_cookie() 
    { 
        $_COOKIE =array(); 
        unset($_COOKIE); 
    } 
}

3.待完善部分

cookie的设置和读取要加密和解密的过程。

 

4.总结

发布评论

分享到:

IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

php文件类讲解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。