1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
|
|
|
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: liu21st <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
declare (strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace think; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Cookie管理类 |
17
|
|
|
*/ |
|
|
|
|
18
|
|
|
class Cookie |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* 配置参数 |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $config = [ |
25
|
|
|
// cookie 保存时间 |
26
|
|
|
'expire' => 0, |
27
|
|
|
// cookie 保存路径 |
28
|
|
|
'path' => '/', |
29
|
|
|
// cookie 有效域名 |
30
|
|
|
'domain' => '', |
31
|
|
|
// cookie 启用安全传输 |
32
|
|
|
'secure' => false, |
33
|
|
|
// httponly设置 |
34
|
|
|
'httponly' => false, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Cookie写入数据 |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $cookie = []; |
42
|
|
|
|
43
|
|
|
/** |
|
|
|
|
44
|
|
|
* 构造方法 |
45
|
|
|
* @access public |
46
|
|
|
*/ |
47
|
8 |
|
public function __construct(array $config = []) |
48
|
|
|
{ |
49
|
8 |
|
$this->config = array_merge($this->config, array_change_key_case($config)); |
50
|
8 |
|
} |
51
|
|
|
|
52
|
8 |
|
public static function __make(Config $config) |
|
|
|
|
53
|
|
|
{ |
54
|
8 |
|
return new static($config->get('cookie')); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* 获取cookie保存数据 |
59
|
|
|
* @access public |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function getCookie(): array |
63
|
|
|
{ |
64
|
|
|
return $this->cookie; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Cookie 设置 |
69
|
|
|
* |
70
|
|
|
* @access public |
71
|
|
|
* @param string $name cookie名称 |
|
|
|
|
72
|
|
|
* @param string $value cookie值 |
|
|
|
|
73
|
|
|
* @param mixed $option 可选参数 |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function set(string $name, string $value, $option = null): void |
77
|
|
|
{ |
78
|
|
|
// 参数设置(会覆盖黙认设置) |
79
|
|
|
if (!is_null($option)) { |
80
|
|
|
if (is_numeric($option)) { |
81
|
|
|
$option = ['expire' => $option]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$config = array_merge($this->config, array_change_key_case($option)); |
85
|
|
|
} else { |
86
|
|
|
$config = $this->config; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0; |
90
|
|
|
|
91
|
|
|
$this->setCookie($name, (string) $value, $expire, $config); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Cookie 保存 |
96
|
|
|
* |
97
|
|
|
* @access public |
98
|
|
|
* @param string $name cookie名称 |
|
|
|
|
99
|
|
|
* @param mixed $value cookie值 |
|
|
|
|
100
|
|
|
* @param int $expire 有效期 |
101
|
|
|
* @param array $option 可选参数 |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
protected function setCookie(string $name, string $value, int $expire, array $option = []): void |
105
|
|
|
{ |
106
|
|
|
$this->cookie[$name] = [$value, $expire, $option]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* 永久保存Cookie数据 |
111
|
|
|
* @access public |
112
|
|
|
* @param string $name cookie名称 |
|
|
|
|
113
|
|
|
* @param string $value cookie值 |
|
|
|
|
114
|
|
|
* @param mixed $option 可选参数 可能会是 null|integer|string |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function forever(string $name, string $value = '', $option = null): void |
118
|
|
|
{ |
119
|
|
|
if (is_null($option) || is_numeric($option)) { |
120
|
|
|
$option = []; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$option['expire'] = 315360000; |
124
|
|
|
|
125
|
|
|
$this->set($name, $value, $option); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Cookie删除 |
130
|
|
|
* @access public |
131
|
|
|
* @param string $name cookie名称 |
132
|
|
|
* @return void |
133
|
|
|
*/ |
134
|
|
|
public function delete(string $name): void |
135
|
|
|
{ |
136
|
|
|
$this->setCookie($name, '', time() - 3600, $this->config); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* 保存Cookie |
141
|
|
|
* @access public |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function save(): void |
145
|
|
|
{ |
146
|
|
|
foreach ($this->cookie as $name => $val) { |
147
|
|
|
list($value, $expire, $option) = $val; |
148
|
|
|
|
149
|
|
|
$this->saveCookie($name, $value, $expire, $option['path'], $option['domain'], $option['secure'] ? true : false, $option['httponly'] ? true : false); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* 保存Cookie |
155
|
|
|
* @access public |
156
|
|
|
* @param string $name cookie名称 |
|
|
|
|
157
|
|
|
* @param string $value cookie值 |
|
|
|
|
158
|
|
|
* @param int $expire cookie过期时间 |
|
|
|
|
159
|
|
|
* @param string $path 有效的服务器路径 |
|
|
|
|
160
|
|
|
* @param string $domain 有效域名/子域名 |
|
|
|
|
161
|
|
|
* @param bool $secure 是否仅仅通过HTTPS |
|
|
|
|
162
|
|
|
* @param bool $httponly 仅可通过HTTP访问 |
163
|
|
|
* @return void |
164
|
|
|
*/ |
165
|
|
|
protected function saveCookie(string $name, string $value, int $expire, string $path, string $domain, bool $secure, bool $httponly): void |
166
|
|
|
{ |
167
|
|
|
setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|