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