1 | <?php |
||
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 | // samesite 设置,支持 'strict' 'lax' |
||
39 | 'samesite' => '', |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * Cookie写入数据 |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $cookie = []; |
||
47 | |||
48 | /** |
||
49 | * 当前Request对象 |
||
50 | * @var Request |
||
51 | */ |
||
52 | protected $request; |
||
53 | |||
54 | /** |
||
55 | * 构造方法 |
||
56 | * @access public |
||
57 | */ |
||
58 | 18 | public function __construct(Request $request, array $config = []) |
|
63 | |||
64 | 18 | public static function __make(Request $request, Config $config) |
|
68 | |||
69 | /** |
||
70 | * 获取cookie |
||
71 | * @access public |
||
72 | * @param mixed $name 数据名称 |
||
73 | * @param string $default 默认值 |
||
74 | * @return mixed |
||
75 | */ |
||
76 | public function get(string $name = '', $default = null) |
||
80 | |||
81 | /** |
||
82 | * 是否存在Cookie参数 |
||
83 | * @access public |
||
84 | * @param string $name 变量名 |
||
85 | * @return bool |
||
86 | */ |
||
87 | public function has(string $name): bool |
||
91 | |||
92 | /** |
||
93 | * Cookie 设置 |
||
94 | * |
||
95 | * @access public |
||
96 | * @param string $name cookie名称 |
||
97 | * @param string $value cookie值 |
||
98 | * @param mixed $option 可选参数 |
||
99 | * @return void |
||
100 | */ |
||
101 | public function set(string $name, string $value, $option = null): void |
||
122 | |||
123 | /** |
||
124 | * Cookie 保存 |
||
125 | * |
||
126 | * @access public |
||
127 | * @param string $name cookie名称 |
||
128 | * @param string $value cookie值 |
||
129 | * @param int $expire 有效期 |
||
130 | * @param array $option 可选参数 |
||
131 | * @return void |
||
132 | */ |
||
133 | protected function setCookie(string $name, string $value, int $expire, array $option = []): void |
||
137 | |||
138 | /** |
||
139 | * 永久保存Cookie数据 |
||
140 | * @access public |
||
141 | * @param string $name cookie名称 |
||
142 | * @param string $value cookie值 |
||
143 | * @param mixed $option 可选参数 可能会是 null|integer|string |
||
144 | * @return void |
||
145 | */ |
||
146 | public function forever(string $name, string $value = '', $option = null): void |
||
156 | |||
157 | /** |
||
158 | * Cookie删除 |
||
159 | * @access public |
||
160 | * @param string $name cookie名称 |
||
161 | * @return void |
||
162 | */ |
||
163 | public function delete(string $name): void |
||
167 | |||
168 | /** |
||
169 | * 获取cookie保存数据 |
||
170 | * @access public |
||
171 | * @return array |
||
172 | */ |
||
173 | public function getCookie(): array |
||
177 | |||
178 | /** |
||
179 | * 保存Cookie |
||
180 | * @access public |
||
181 | * @return void |
||
182 | */ |
||
183 | public function save(): void |
||
200 | |||
201 | /** |
||
202 | * 保存Cookie |
||
203 | * @access public |
||
204 | * @param string $name cookie名称 |
||
205 | * @param string $value cookie值 |
||
206 | * @param int $expire cookie过期时间 |
||
207 | * @param string $path 有效的服务器路径 |
||
208 | * @param string $domain 有效域名/子域名 |
||
209 | * @param bool $secure 是否仅仅通过HTTPS |
||
210 | * @param bool $httponly 仅可通过HTTP访问 |
||
211 | * @param string $samesite 防止CSRF攻击和用户追踪 |
||
212 | * @return void |
||
213 | */ |
||
214 | protected function saveCookie(string $name, string $value, int $expire, string $path, string $domain, bool $secure, bool $httponly, string $samesite): void |
||
229 | |||
230 | } |
||
231 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.