269 lines
7.3 KiB
PHP
Executable File
269 lines
7.3 KiB
PHP
Executable File
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||
// +----------------------------------------------------------------------
|
||
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
|
||
// +----------------------------------------------------------------------
|
||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||
// +----------------------------------------------------------------------
|
||
// | Author: liu21st <liu21st@gmail.com>
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace think;
|
||
|
||
class Cookie
|
||
{
|
||
/**
|
||
* 配置参数
|
||
* @var array
|
||
*/
|
||
protected $config = [
|
||
// cookie 名称前缀
|
||
'prefix' => '',
|
||
// cookie 保存时间
|
||
'expire' => 0,
|
||
// cookie 保存路径
|
||
'path' => '/',
|
||
// cookie 有效域名
|
||
'domain' => '',
|
||
// cookie 启用安全传输
|
||
'secure' => false,
|
||
// httponly设置
|
||
'httponly' => false,
|
||
// 是否使用 setcookie
|
||
'setcookie' => true,
|
||
];
|
||
|
||
/**
|
||
* 构造方法
|
||
* @access public
|
||
*/
|
||
public function __construct(array $config = [])
|
||
{
|
||
$this->init($config);
|
||
}
|
||
|
||
/**
|
||
* Cookie初始化
|
||
* @access public
|
||
* @param array $config
|
||
* @return void
|
||
*/
|
||
public function init(array $config = [])
|
||
{
|
||
$this->config = array_merge($this->config, array_change_key_case($config));
|
||
|
||
if (!empty($this->config['httponly']) && PHP_SESSION_ACTIVE != session_status()) {
|
||
ini_set('session.cookie_httponly', 1);
|
||
}
|
||
}
|
||
|
||
public static function __make(Config $config)
|
||
{
|
||
return new static($config->pull('cookie'));
|
||
}
|
||
|
||
/**
|
||
* 设置或者获取cookie作用域(前缀)
|
||
* @access public
|
||
* @param string $prefix
|
||
* @return string|void
|
||
*/
|
||
public function prefix($prefix = '')
|
||
{
|
||
if (empty($prefix)) {
|
||
return $this->config['prefix'];
|
||
}
|
||
|
||
$this->config['prefix'] = $prefix;
|
||
}
|
||
|
||
/**
|
||
* Cookie 设置、获取、删除
|
||
*
|
||
* @access public
|
||
* @param string $name cookie名称
|
||
* @param mixed $value cookie值
|
||
* @param mixed $option 可选参数 可能会是 null|integer|string
|
||
* @return void
|
||
*/
|
||
public 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($this->config, array_change_key_case($option));
|
||
} else {
|
||
$config = $this->config;
|
||
}
|
||
|
||
$name = $config['prefix'] . $name;
|
||
|
||
// 设置cookie
|
||
if (is_array($value)) {
|
||
array_walk_recursive($value, [$this, 'jsonFormatProtect'], 'encode');
|
||
$value = 'think:' . json_encode($value);
|
||
}
|
||
|
||
$expire = !empty($config['expire']) ? $_SERVER['REQUEST_TIME'] + intval($config['expire']) : 0;
|
||
|
||
if ($config['setcookie']) {
|
||
$this->setCookie($name, $value, $expire, $config);
|
||
}
|
||
|
||
$_COOKIE[$name] = $value;
|
||
}
|
||
|
||
/**
|
||
* Cookie 设置保存
|
||
*
|
||
* @access public
|
||
* @param string $name cookie名称
|
||
* @param mixed $value cookie值
|
||
* @param array $option 可选参数
|
||
* @return void
|
||
*/
|
||
protected function setCookie($name, $value, $expire, $option = [])
|
||
{
|
||
setcookie($name, $value, $expire, $option['path'], $option['domain'], $option['secure'], $option['httponly']);
|
||
}
|
||
|
||
/**
|
||
* 永久保存Cookie数据
|
||
* @access public
|
||
* @param string $name cookie名称
|
||
* @param mixed $value cookie值
|
||
* @param mixed $option 可选参数 可能会是 null|integer|string
|
||
* @return void
|
||
*/
|
||
public function forever($name, $value = '', $option = null)
|
||
{
|
||
if (is_null($option) || is_numeric($option)) {
|
||
$option = [];
|
||
}
|
||
|
||
$option['expire'] = 315360000;
|
||
|
||
$this->set($name, $value, $option);
|
||
}
|
||
|
||
/**
|
||
* 判断Cookie数据
|
||
* @access public
|
||
* @param string $name cookie名称
|
||
* @param string|null $prefix cookie前缀
|
||
* @return bool
|
||
*/
|
||
public function has($name, $prefix = null)
|
||
{
|
||
$prefix = !is_null($prefix) ? $prefix : $this->config['prefix'];
|
||
$name = $prefix . $name;
|
||
|
||
return isset($_COOKIE[$name]);
|
||
}
|
||
|
||
/**
|
||
* Cookie获取
|
||
* @access public
|
||
* @param string $name cookie名称 留空获取全部
|
||
* @param string|null $prefix cookie前缀
|
||
|