企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] * * * * * ## 1 Cookie文件源代码(thinkphp/library/think/Cookie.php) ~~~ protected static $config = [ 'prefix' => '', 'expire' => 0, 'path' => '/', 'domain' => '', 'secure' => false, 'httponly' => '', 'setcookie' => true, ]; ~~~ ~~~ public static function init(array $config = []) { self::$config = array_merge(self::$config, array_change_key_case($config)); if (!empty(self::$config['httponly'])) { ini_set('session.cookie_httponly', 1); } } ~~~ ~~~ public static function prefix($prefix = '') { if (empty($prefix)) { return self::$config['prefix']; } self::$config['prefix'] = $prefix; } ~~~ ~~~ public static function set($name, $value = '', $option = null) { if (!is_null($option)) { if (is_numeric($option)) { $option = ['expire' => $option]; } elseif (is_string($option)) { parse_str($option, $option); } $config = array_merge(self::$config, array_change_key_case($option)); } else { $config = self::$config; } $name = $config['prefix'] . $name; if (is_array($value)) { array_walk_recursive($value, 'self::jsonFormatProtect', 'encode'); $value = 'think:' . json_encode($value); } $expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0; if (self::$config['setcookie']) { setcookie($name, $value, $expire, $config['path'], $config['domain'], $config['secure'], $config['httponly']); } $_COOKIE[$name] = $value; } ~~~ ~~~ public static function get($name, $prefix = null) { $prefix = !is_null($prefix) ? $prefix : self::$config['prefix']; $name = $prefix . $name; if (isset($_COOKIE[$name])) { $value = $_COOKIE[$name]; if (0 === strpos($value, 'think:')) { $value = substr($value, 6); $value = json_decode($value, true); array_walk_recursive($value, 'self::jsonFormatProtect', 'decode'); } return $value; } else { return null; } } ~~~ ~~~ public static function delete($name, $prefix = null) { $config = self::$config; $prefix = !is_null($prefix) ? $prefix : $config['prefix']; $name = $prefix . $name; if (self::$config['setcookie']) { setcookie($name, '', time() - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']); } unset($_COOKIE[$name]); } ~~~ ~~~ public static function clear($prefix = null) { if (empty($_COOKIE)) { return; } $config = self::$config; $prefix = !is_null($prefix) ? $prefix : $config['prefix']; if ($prefix) { foreach ($_COOKIE as $key => $val) { if (0 === strpos($key, $prefix)) { if (self::$config['setcookie']) { setcookie($key, '', time() - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']); } unset($_COOKIE[$key]); } } } else { unset($_COOKIE); } return; } ~~~ ~~~ private static function jsonFormatProtect(&$val, $key, $type = 'encode') { if (!empty($val) && true !== $val) { $val = 'decode' == $type ? urldecode($val) : urlencode($val); } } ~~~ ## 2 分析 1 `$config` cookie配置参数 cookie相关配参数置见基础原理的网络通信之cookie 2 `public static function init(array $config = []){}` cookie初始化。 3 `public static function prefix($prefix = ''){}` cookie前缀设置 4 `public static function set($name, $value = '', $option = null){}` 设置cookie的配置参数,设置cookie的$name=>$value值对 5 `public static function get($name, $prefix = null){}` 获取cookie的$name对应的值 6 `public static function delete($name, $prefix = null){}` 删除cookie的$name 7 `public static function clear($prefix = null){}` 清空所有cookie 8 `private static function jsonFormatProtect(&$val, $key, $type = 'encode')` 数据格式转换,在set,get中调用 ## 3 使用方法 1 cookie快速操作 在helper.php中封装了 cookie()方法来快速操作Cookie 与tp3.2类似,使用方法见[官方手册](http://www.kancloud.cn/manual/thinkphp/1873) ## 4 总结 2个配置操作函数:init() prefix() 4个cookie数据操作函数:set() get() delete() clear() 1个私有格式转换函数:jsonFormatProtect() 总共6个公共可调用方法,1个私有方法。