mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
改进cookie类
This commit is contained in:
@@ -16,17 +16,19 @@ class Cookie
|
|||||||
|
|
||||||
protected static $config = [
|
protected static $config = [
|
||||||
// cookie 名称前缀
|
// cookie 名称前缀
|
||||||
'prefix' => '',
|
'prefix' => '',
|
||||||
// cookie 保存时间
|
// cookie 保存时间
|
||||||
'expire' => 0,
|
'expire' => 0,
|
||||||
// cookie 保存路径
|
// cookie 保存路径
|
||||||
'path' => '/',
|
'path' => '/',
|
||||||
// cookie 有效域名
|
// cookie 有效域名
|
||||||
'domain' => '',
|
'domain' => '',
|
||||||
// cookie 启用安全传输
|
// cookie 启用安全传输
|
||||||
'secure' => false,
|
'secure' => false,
|
||||||
// httponly设置
|
// httponly设置
|
||||||
'httponly' => '',
|
'httponly' => '',
|
||||||
|
// 是否使用 setcookie
|
||||||
|
'setcookie' => true,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -86,7 +88,9 @@ class Cookie
|
|||||||
$value = 'think:' . json_encode($value);
|
$value = 'think:' . json_encode($value);
|
||||||
}
|
}
|
||||||
$expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
|
$expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
|
||||||
setcookie($name, $value, $expire, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
|
if (self::$config['setcookie']) {
|
||||||
|
setcookie($name, $value, $expire, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
|
||||||
|
}
|
||||||
$_COOKIE[$name] = $value;
|
$_COOKIE[$name] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,7 +128,9 @@ class Cookie
|
|||||||
$config = self::$config;
|
$config = self::$config;
|
||||||
$prefix = $prefix ? $prefix : $config['prefix'];
|
$prefix = $prefix ? $prefix : $config['prefix'];
|
||||||
$name = $prefix . $name;
|
$name = $prefix . $name;
|
||||||
setcookie($name, '', time() - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
|
if (self::$config['setcookie']) {
|
||||||
|
setcookie($name, '', time() - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
|
||||||
|
}
|
||||||
// 删除指定cookie
|
// 删除指定cookie
|
||||||
unset($_COOKIE[$name]);
|
unset($_COOKIE[$name]);
|
||||||
}
|
}
|
||||||
@@ -148,7 +154,9 @@ class Cookie
|
|||||||
// 如果前缀为空字符串将不作处理直接返回
|
// 如果前缀为空字符串将不作处理直接返回
|
||||||
foreach ($_COOKIE as $key => $val) {
|
foreach ($_COOKIE as $key => $val) {
|
||||||
if (0 === strpos($key, $prefix)) {
|
if (0 === strpos($key, $prefix)) {
|
||||||
setcookie($key, '', time() - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
|
if (self::$config['setcookie']) {
|
||||||
|
setcookie($key, '', time() - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
|
||||||
|
}
|
||||||
unset($_COOKIE[$key]);
|
unset($_COOKIE[$key]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,17 +24,19 @@ class cookieTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
protected $default = [
|
protected $default = [
|
||||||
// cookie 名称前缀
|
// cookie 名称前缀
|
||||||
'prefix' => '',
|
'prefix' => '',
|
||||||
// cookie 保存时间
|
// cookie 保存时间
|
||||||
'expire' => 0,
|
'expire' => 0,
|
||||||
// cookie 保存路径
|
// cookie 保存路径
|
||||||
'path' => '/',
|
'path' => '/',
|
||||||
// cookie 有效域名
|
// cookie 有效域名
|
||||||
'domain' => '',
|
'domain' => '',
|
||||||
// cookie 启用安全传输
|
// cookie 启用安全传输
|
||||||
'secure' => false,
|
'secure' => false,
|
||||||
// httponly设置
|
// httponly设置
|
||||||
'httponly' => '',
|
'httponly' => '',
|
||||||
|
// 是否使用 setcookie
|
||||||
|
'setcookie' => false,
|
||||||
];
|
];
|
||||||
|
|
||||||
protected function setUp()
|
protected function setUp()
|
||||||
|
|||||||
Reference in New Issue
Block a user