@@ -26,6 +26,10 @@ class Cookie
26
26
protected $ secure ;
27
27
protected $ httpOnly ;
28
28
private $ raw ;
29
+ private $ sameSite ;
30
+
31
+ const SAMESITE_LAX = 'lax ' ;
32
+ const SAMESITE_STRICT = 'strict ' ;
29
33
30
34
/**
31
35
* Constructor.
@@ -38,10 +42,11 @@ class Cookie
38
42
* @param bool $secure Whether the cookie should only be transmitted over a secure HTTPS connection from the client
39
43
* @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
40
44
* @param bool $raw Whether the cookie value should be sent with no url encoding
45
+ * @param string|null $sameSite Whether the cookie will be available for cross-site requests
41
46
*
42
47
* @throws \InvalidArgumentException
43
48
*/
44
- public function __construct ($ name , $ value = null , $ expire = 0 , $ path = '/ ' , $ domain = null , $ secure = false , $ httpOnly = true , $ raw = false )
49
+ public function __construct ($ name , $ value = null , $ expire = 0 , $ path = '/ ' , $ domain = null , $ secure = false , $ httpOnly = true , $ raw = false , $ sameSite = null )
45
50
{
46
51
// from PHP source code
47
52
if (preg_match ("/[=,; \t\r\n\013\014]/ " , $ name )) {
@@ -71,6 +76,12 @@ public function __construct($name, $value = null, $expire = 0, $path = '/', $dom
71
76
$ this ->secure = (bool ) $ secure ;
72
77
$ this ->httpOnly = (bool ) $ httpOnly ;
73
78
$ this ->raw = (bool ) $ raw ;
79
+
80
+ if (!in_array ($ sameSite , array (self ::SAMESITE_LAX , self ::SAMESITE_STRICT , null ))) {
81
+ throw new \InvalidArgumentException ('The sameSite parameter is not valid. ' );
82
+ }
83
+
84
+ $ this ->sameSite = $ sameSite ;
74
85
}
75
86
76
87
/**
@@ -108,6 +119,10 @@ public function __toString()
108
119
$ str .= '; httponly ' ;
109
120
}
110
121
122
+ if (null !== $ this ->getSameSite ()) {
123
+ $ str .= '; samesite= ' .$ this ->getSameSite ();
124
+ }
125
+
111
126
return $ str ;
112
127
}
113
128
@@ -200,4 +215,14 @@ public function isRaw()
200
215
{
201
216
return $ this ->raw ;
202
217
}
218
+
219
+ /**
220
+ * Gets the SameSite attribute.
221
+ *
222
+ * @return string|null
223
+ */
224
+ public function getSameSite ()
225
+ {
226
+ return $ this ->sameSite ;
227
+ }
203
228
}
0 commit comments