Skip to content

Commit f563df5

Browse files
authored
Merge pull request #570 from sentrychen/cookie-samesite
2 parents cfc0927 + b23d273 commit f563df5

1 file changed

Lines changed: 41 additions & 4 deletions

File tree

src/http-message/src/Cookie.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Swoft\Stdlib\Helper\ObjectHelper;
77
use function bean;
88
use function gmdate;
9+
use function in_array;
910
use function urlencode;
1011

1112
/**
@@ -27,8 +28,14 @@ class Cookie
2728
'secure' => false,
2829
'httpOnly' => false,
2930
'hostOnly' => false,
31+
'sameSite' => ''
3032
];
3133

34+
/**
35+
* SameSite Values
36+
*/
37+
public const SAME_SITE_VALUES = ['Strict', 'Lax', 'None'];
38+
3239
/**
3340
* @var string
3441
*/
@@ -69,6 +76,11 @@ class Cookie
6976
*/
7077
private $httpOnly = false;
7178

79+
/**
80+
* @var string
81+
*/
82+
private $sameSite = '';
83+
7284
/**
7385
* @param array $config
7486
*
@@ -98,6 +110,7 @@ public function toArray(): array
98110
'secure' => $this->secure,
99111
'httpOnly' => $this->httpOnly,
100112
'hostOnly' => $this->hostOnly,
113+
'sameSite' => $this->sameSite,
101114
];
102115
}
103116

@@ -124,14 +137,14 @@ public function toString(): string
124137
$result .= '; expires=' . gmdate('D, d-M-Y H:i:s e', $timestamp);
125138
}
126139

140+
if ($this->sameSite) {
141+
$result .= '; SameSite=' . $this->sameSite;
142+
}
143+
127144
if ($this->secure) {
128145
$result .= '; secure';
129146
}
130147

131-
// if ($hostOnly) {
132-
// $result .= '; HostOnly';
133-
// }
134-
135148
if ($this->httpOnly) {
136149
$result .= '; HttpOnly';
137150
}
@@ -307,4 +320,28 @@ public function setHttpOnly(bool $httpOnly): Cookie
307320
$this->httpOnly = $httpOnly;
308321
return $this;
309322
}
323+
324+
/**
325+
* @return string
326+
*/
327+
public function getSameSite(): string
328+
{
329+
return $this->sameSite;
330+
}
331+
332+
/**
333+
* @param string $sameSite
334+
*
335+
* @return Cookie
336+
*/
337+
public function setSameSite(string $sameSite): Cookie
338+
{
339+
if (in_array($sameSite, static::SAME_SITE_VALUES, true)) {
340+
$this->sameSite = $sameSite;
341+
} else {
342+
$this->sameSite = '';
343+
}
344+
345+
return $this;
346+
}
310347
}

0 commit comments

Comments
 (0)