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
|
|
|
use DateTimeInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Cookie管理类 |
19
|
|
|
* @package think |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
class Cookie |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* 配置参数 |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $config = [ |
28
|
|
|
// cookie 保存时间 |
29
|
|
|
'expire' => 0, |
30
|
|
|
// cookie 保存路径 |
31
|
|
|
'path' => '/', |
32
|
|
|
// cookie 有效域名 |
33
|
|
|
'domain' => '', |
34
|
|
|
// cookie 启用安全传输 |
35
|
|
|
'secure' => false, |
36
|
|
|
// httponly设置 |
37
|
|
|
'httponly' => false, |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Cookie写入数据 |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $cookie = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* 当前Request对象 |
48
|
|
|
* @var Request |
49
|
|
|
*/ |
50
|
|
|
protected $request; |
51
|
|
|
|
52
|
|
|
/** |
|
|
|
|
53
|
|
|
* 构造方法 |
54
|
|
|
* @access public |
55
|
|
|
*/ |
56
|
|
|
public function __construct(Request $request, array $config = []) |
57
|
|
|
{ |
58
|
|
|
$this->request = $request; |
59
|
|
|
$this->config = array_merge($this->config, array_change_key_case($config)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function __make(Request $request, Config $config) |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
return new static($request, $config->get('cookie')); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* 获取cookie |
69
|
|
|
* @access public |
70
|
|
|
* @param mixed $name 数据名称 |
|
|
|
|
71
|
|
|
* @param string $default 默认值 |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function get(string $name = '', $default = null) |
75
|
|
|
{ |
76
|
|
|
return $this->request->cookie($name, $default); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* 是否存在Cookie参数 |
81
|
|
|
* @access public |
82
|
|
|
* @param string $name 变量名 |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function has(string $name): bool |
86
|
|
|
{ |
87
|
|
|
return $this->request->has($name, 'cookie'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Cookie 设置 |
92
|
|
|
* |
93
|
|
|
* @access public |
94
|
|
|
* @param string $name cookie名称 |
|
|
|
|
95
|
|
|
* @param string $value cookie值 |
|
|
|
|
96
|
|
|
* @param mixed $option 可选参数 |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function set(string $name, string $value, $option = null): void |
100
|
|
|
{ |
101
|
|
|
// 参数设置(会覆盖黙认设置) |
102
|
|
|
if (!is_null($option)) { |
103
|
|
|
if (is_numeric($option) || $option instanceof DateTimeInterface) { |
104
|
|
|
$option = ['expire' => $option]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$config = array_merge($this->config, array_change_key_case($option)); |
108
|
|
|
} else { |
109
|
|
|
$config = $this->config; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($config['expire'] instanceof DateTimeInterface) { |
113
|
|
|
$expire = $config['expire']->getTimestamp(); |
114
|
|
|
} else { |
115
|
|
|
$expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->setCookie($name, $value, $expire, $config); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Cookie 保存 |
123
|
|
|
* |
124
|
|
|
* @access public |
125
|
|
|
* @param string $name cookie名称 |
|
|
|
|
126
|
|
|
* @param string $value cookie值 |
|
|
|
|
127
|
|
|
* @param int $expire 有效期 |
128
|
|
|
* @param array $option 可选参数 |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
protected function setCookie(string $name, string $value, int $expire, array $option = []): void |
132
|
|
|
{ |
133
|
|
|
$this->cookie[$name] = [$value, $expire, $option]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* 永久保存Cookie数据 |
138
|
|
|
* @access public |
139
|
|
|
* @param string $name cookie名称 |
|
|
|
|
140
|
|
|
* @param string $value cookie值 |
|
|
|
|
141
|
|
|
* @param mixed $option 可选参数 可能会是 null|integer|string |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
|
|
public function forever(string $name, string $value = '', $option = null): void |
145
|
|
|
{ |
146
|
|
|
if (is_null($option) || is_numeric($option)) { |
147
|
|
|
$option = []; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$option['expire'] = 315360000; |
151
|
|
|
|
152
|
|
|
$this->set($name, $value, $option); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Cookie删除 |
157
|
|
|
* @access public |
158
|
|
|
* @param string $name cookie名称 |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
public function delete(string $name): void |
162
|
|
|
{ |
163
|
|
|
$this->setCookie($name, '', time() - 3600, $this->config); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* 获取cookie保存数据 |
168
|
|
|
* @access public |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
public function getCookie(): array |
172
|
|
|
{ |
173
|
|
|
return $this->cookie; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* 保存Cookie |
178
|
|
|
* @access public |
179
|
|
|
* @return void |
180
|
|
|
*/ |
181
|
|
|
public function save(): void |
182
|
|
|
{ |
183
|
|
|
foreach ($this->cookie as $name => $val) { |
184
|
|
|
list($value, $expire, $option) = $val; |
185
|
|
|
|
186
|
|
|
$this->saveCookie($name, $value, $expire, $option['path'], $option['domain'], $option['secure'] ? true : false, $option['httponly'] ? true : false); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* 保存Cookie |
192
|
|
|
* @access public |
193
|
|
|
* @param string $name cookie名称 |
|
|
|
|
194
|
|
|
* @param string $value cookie值 |
|
|
|
|
195
|
|
|
* @param int $expire cookie过期时间 |
|
|
|
|
196
|
|
|
* @param string $path 有效的服务器路径 |
|
|
|
|
197
|
|
|
* @param string $domain 有效域名/子域名 |
|
|
|
|
198
|
|
|
* @param bool $secure 是否仅仅通过HTTPS |
|
|
|
|
199
|
|
|
* @param bool $httponly 仅可通过HTTP访问 |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
protected function saveCookie(string $name, string $value, int $expire, string $path, string $domain, bool $secure, bool $httponly): void |
203
|
|
|
{ |
204
|
|
|
setcookie($name, $value, $expire, $path, $domain, $secure, $httponly); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
} |
208
|
|
|
|