• phpcookie类


    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.总结

  • 相关阅读:
    Python 基础(二)
    3.6:手写代码题(包含sql题)
    3.2:负载均衡、集群相关
    3.1:并发、安全与性能调优
    2.6:Linux/Shell脚本
    2.5:Git/Svn
    2.4:缓存
    2.3:消息中间件
    2.2:数据库
    2.1:常用框架
  • 原文地址:https://www.cnblogs.com/norm/p/6246076.html
Copyright © 2020-2023  润新知