您的位置:首页 > 编程语言 > PHP开发

[李景山php]每天TP5-20161230|thinkphp5-Cookie.php

2016-11-25 08:54 447 查看
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace think;
// 根包 之下
class Cookie
{//Cookie 二次封装
protected static $config = [// cookie 配置项
// cookie 名称前缀
'prefix'    => '',// cookie 名称前缀
// cookie 保存时间
'expire'    => 0,// cookie 保存时间
// cookie 保存路径
'path'      => '/', // 保存路径
// cookie 有效域名
'domain'    => '', // 有效域名
//  cookie 启用安全传输
'secure'    => false,// 启用安全传输
// httponly设置
'httponly'  => '',// httponly 设置
// 是否使用 setcookie
'setcookie' => true, // 使用 setcookie
];

protected static $init;// 静态初始化 变量

/**
* Cookie初始化
* @param array $config
* @return void
*/
public static function init(array $config = [])// Cookie 初始化
{
if (empty($config)) {// 如果配置参数为空
$config = Config::get('cookie');// 获取配置选项
}
self::$config = array_merge(self::$config, array_change_key_case($config));// 配置项合并
if (!empty(self::$config['httponly'])) {// 如果非空设置 httponly
ini_set('session.cookie_httponly', 1);// 使用设置 session 配置项
}
self::$init = true;// 设置初始化完成
}

/**
* 设置或者获取cookie作用域(前缀)
* @param string $prefix
* @return string|void
*/
public static function prefix($prefix = '')// 设置 或者 获取 作用域 前缀
{
if (empty($prefix)) {
return self::$config['prefix'];
}
self::$config['prefix'] = $prefix;
}

/**
* Cookie 设置、获取、删除
*
* @param string $name  cookie名称
* @param mixed  $value cookie值
* @param mixed  $option 可选参数 可能会是 null|integer|string
*
* @return mixed
* @internal param mixed $options cookie参数
*/
public static function set($name, $value = '', $option = null)// 设置 获取 删除
{
!isset(self::$init) && self::init();// 确认初始化 这个用法也不错
// 参数设置(会覆盖黙认设置)
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;// 设置cookie 存储名字
// 设置cookie
if (is_array($value)) {// 存储 值是 数组的数据,先格式化,然后输出
array_walk_recursive($value, 'self::jsonFormatProtect', 'encode');
$value = 'think:' . json_encode($value);
}
$expire = !empty($config['expire']) ? $_SERVER['REQUEST_TIME'] + intval($config['expire']) : 0;// 设置过期时间
if ($config['setcookie']) {// 通过 setcookie 方式设置
setcookie($name, $value, $expire, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
}
$_COOKIE[$name] = $value;// 普通赋值方式
}

/**
* 判断Cookie数据
* @param string        $name cookie名称
* @param string|null   $prefix cookie前缀
* @return bool
*/
public static function has($name, $prefix = null)
{
!isset(self::$init) && self::init();
$prefix = !is_null($prefix) ? $prefix : self::$config['prefix'];
$name   = $prefix . $name;
return isset($_COOKIE[$name]);// 判读是否有值
}

/**
* Cookie获取
* @param string        $name cookie名称
* @param string|null   $prefix cookie前缀
* @return mixed
*/
public static function get($name, $prefix = null)
{
!isset(self::$init) && self::init();// 初始化
$prefix = !is_null($prefix) ? $prefix : self::$config['prefix'];// 获取 cookie 存储 key
$name   = $prefix . $name;// 获取完成
if (isset($_COOKIE[$name])) {//如有有数据
$value = $_COOKIE[$name];
if (0 === strpos($value, 'think:')) {// 如果有 think: 并且是开头位置
$value = substr($value, 6);
$value = json_decode($value, true);
array_walk_recursive($value, 'self::jsonFormatProtect', 'decode');// 返回数据
}
return $value;
} else {
return null;
}
}

/**
* Cookie删除
* @param string        $name cookie名称
* @param string|null   $prefix cookie前缀
* @return mixed
*/
public static function delete($name, $prefix = null)
{
!isset(self::$init) && self::init();
$config = self::$config;
$prefix = !is_null($prefix) ? $prefix : $config['prefix'];
$name   = $prefix . $name;
if ($config['setcookie']) {// 删除输出
setcookie($name, '', $_SERVER['REQUEST_TIME'] - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
}
// 删除指定cookie
unset($_COOKIE[$name]);// 删除 数据
}

/**
* Cookie清空
* @param string|null $prefix cookie前缀
* @return mixed
*/
public static function clear($prefix = null)
{
// 清除指定前缀的所有cookie
if (empty($_COOKIE)) {
return;
}// 为空,自动清除
!isset(self::$init) && self::init();
// 要删除的cookie前缀,不指定则删除config设置的指定前缀
$config = self::$config;
$prefix = !is_null($prefix) ? $prefix : $config['prefix'];
if ($prefix) {// 清除 全部带前缀 的 cookie
// 如果前缀为空字符串将不作处理直接返回
foreach ($_COOKIE as $key => $val) {
if (0 === strpos($key, $prefix)) {
if ($config['setcookie']) {// 清除自己的cookie
setcookie($key, '', $_SERVER['REQUEST_TIME'] - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
}
unset($_COOKIE[$key]);
}
}
}
return;
}

private static function jsonFormatProtect(&$val, $key, $type = 'encode')
{// 弄个半天是假的
if (!empty($val) && true !== $val) {
$val = 'decode' == $type ? urldecode($val) : urlencode($val);
}
}

}
// 总结,这个就关注了几个事情
//1 由于增加了 前缀,需要重新封装一遍
//2 根据 setcookie 函数 是否好用, 选择不同的操作方式
//3 设置了 httponly 的方式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thinkphp php