1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license https://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\web; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Cookie represents information related with a cookie, such as [[name]], [[value]], [[domain]], etc. |
12
|
|
|
* |
13
|
|
|
* For more details and usage information on Cookie, see the [guide article on handling cookies](guide:runtime-sessions-cookies). |
14
|
|
|
* |
15
|
|
|
* @author Qiang Xue <[email protected]> |
16
|
|
|
* @since 2.0 |
17
|
|
|
*/ |
18
|
|
|
class Cookie extends \yii\base\BaseObject |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* SameSite policy Lax will prevent the cookie from being sent by the browser in all cross-site browsing context |
22
|
|
|
* during CSRF-prone request methods (e.g. POST, PUT, PATCH etc). |
23
|
|
|
* E.g. a POST request from https://otherdomain.com to https://yourdomain.com will not include the cookie, however a GET request will. |
24
|
|
|
* When a user follows a link from https://otherdomain.com to https://yourdomain.com it will include the cookie |
25
|
|
|
* @see sameSite |
26
|
|
|
*/ |
27
|
|
|
const SAME_SITE_LAX = 'Lax'; |
28
|
|
|
/** |
29
|
|
|
* SameSite policy Strict will prevent the cookie from being sent by the browser in all cross-site browsing context |
30
|
|
|
* regardless of the request method and even when following a regular link. |
31
|
|
|
* E.g. a GET request from https://otherdomain.com to https://yourdomain.com or a user following a link from |
32
|
|
|
* https://otherdomain.com to https://yourdomain.com will not include the cookie. |
33
|
|
|
* @see sameSite |
34
|
|
|
*/ |
35
|
|
|
const SAME_SITE_STRICT = 'Strict'; |
36
|
|
|
/** |
37
|
|
|
* SameSite policy None disables the SameSite policy so cookies will be sent in all contexts, |
38
|
|
|
* i.e in responses to both first-party and cross-origin requests. |
39
|
|
|
* E.g. a POST request from https://otherdomain.com to https://yourdomain.com will include the cookie. |
40
|
|
|
* Note: If `sameSite` is set to None, the `secure` attribute must be set to `true` (otherwise the cookie will be blocked by the browser). |
41
|
|
|
* @see sameSite |
42
|
|
|
* @see secure |
43
|
|
|
* @since 2.0.43 |
44
|
|
|
*/ |
45
|
|
|
const SAME_SITE_NONE = 'None'; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string name of the cookie |
49
|
|
|
*/ |
50
|
|
|
public $name; |
51
|
|
|
/** |
52
|
|
|
* @var string value of the cookie |
53
|
|
|
*/ |
54
|
|
|
public $value = ''; |
55
|
|
|
/** |
56
|
|
|
* @var string domain of the cookie |
57
|
|
|
*/ |
58
|
|
|
public $domain = ''; |
59
|
|
|
/** |
60
|
|
|
* @var int|string|\DateTimeInterface|null the timestamp or date at which the cookie expires. This is the server timestamp. |
61
|
|
|
* Defaults to 0, meaning "until the browser is closed" (the same applies to `null`). |
62
|
|
|
*/ |
63
|
|
|
public $expire = 0; |
64
|
|
|
/** |
65
|
|
|
* @var string the path on the server in which the cookie will be available on. The default is '/'. |
66
|
|
|
*/ |
67
|
|
|
public $path = '/'; |
68
|
|
|
/** |
69
|
|
|
* @var bool whether cookie should be sent via secure connection |
70
|
|
|
*/ |
71
|
|
|
public $secure = false; |
72
|
|
|
/** |
73
|
|
|
* @var bool whether the cookie should be accessible only through the HTTP protocol. |
74
|
|
|
* By setting this property to true, the cookie will not be accessible by scripting languages, |
75
|
|
|
* such as JavaScript, which can effectively help to reduce identity theft through XSS attacks. |
76
|
|
|
*/ |
77
|
|
|
public $httpOnly = true; |
78
|
|
|
/** |
79
|
|
|
* @var string SameSite prevents the browser from sending this cookie along with cross-site requests. |
80
|
|
|
* |
81
|
|
|
* See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite for more information about sameSite. |
82
|
|
|
* |
83
|
|
|
* @since 2.0.21 |
84
|
|
|
*/ |
85
|
|
|
public $sameSite = self::SAME_SITE_LAX; |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Magic method to turn a cookie object into a string without having to explicitly access [[value]]. |
90
|
|
|
* |
91
|
|
|
* ```php |
92
|
|
|
* if (isset($request->cookies['name'])) { |
93
|
|
|
* $value = (string) $request->cookies['name']; |
94
|
|
|
* } |
95
|
|
|
* ``` |
96
|
|
|
* |
97
|
|
|
* @return string The value of the cookie. If the value property is null, an empty string will be returned. |
98
|
|
|
*/ |
99
|
|
|
public function __toString() |
100
|
|
|
{ |
101
|
|
|
return (string) $this->value; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|