if ($value['expireDate'] instanceof \DateTime) {
$expireDate = $value['expireDate']->getTimestamp();
}
+ $sameSite = $value['sameSite'] ?? 'Lax';
+
$io->setCookie(
$name,
$value['value'],
$container->getServer()->getWebRoot(),
null,
$container->getServer()->getRequest()->getServerProtocol() === 'https',
- true
+ true,
+ $sameSite
);
}
* @param bool $secure
* @param bool $httpOnly
*/
- public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly) {
+ public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite = 'Lax') {
$path = $this->webRoot ? : '/';
- setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
+
+ if (PHP_VERSION_ID < 70300) {
+ setcookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
+ } else {
+ setcookie($name, $value, [
+ 'expires' => $expire,
+ 'path' => $path,
+ 'domain' => $domain,
+ 'secure' => $secure,
+ 'httponly' => $httpOnly,
+ 'samesite' => $sameSite
+ ]);
+ }
}
}
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
+ * @param string $sameSite (added in 20)
* @since 8.1.0
*/
- public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly);
+ public function setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite = 'Lax');
}
* @param \DateTime|null $expireDate Date on that the cookie should expire, if set
* to null cookie will be considered as session
* cookie.
+ * @param string $sameSite The samesite value of the cookie. Defaults to Lax. Other possibilities are Strict or None
* @return $this
* @since 8.0.0
*/
- public function addCookie($name, $value, \DateTime $expireDate = null) {
- $this->cookies[$name] = ['value' => $value, 'expireDate' => $expireDate];
+ public function addCookie($name, $value, \DateTime $expireDate = null, $sameSite = 'Lax') {
+ $this->cookies[$name] = ['value' => $value, 'expireDate' => $expireDate, 'sameSite' => $sameSite];
return $this;
}
'foo' => [
'value' => 'bar',
'expireDate' => null,
+ 'sameSite' => 'Lax',
],
'bar' => [
'value' => 'foo',
- 'expireDate' => new \DateTime('1970-01-01')
+ 'expireDate' => new \DateTime('1970-01-01'),
+ 'sameSite' => 'Lax',
]
];
$this->assertEquals($expectedResponse, $this->childResponse->getCookies());
$expected = [
'foo' => [
'value' => 'expired',
- 'expireDate' => new \DateTime('1971-01-01')
+ 'expireDate' => new \DateTime('1971-01-01'),
+ 'sameSite' => 'Lax',
]
];
$expected = [
'foo' => [
'value' => 'bar',
- 'expireDate' => null
+ 'expireDate' => null,
+ 'sameSite' => 'Lax',
],
'bar' => [
'value' => 'foo',
- 'expireDate' => null
+ 'expireDate' => null,
+ 'sameSite' => 'Lax',
]
];
$cookies = $this->childResponse->getCookies();
$expected = [
'foo' => [
'value' => 'expired',
- 'expireDate' => new \DateTime('1971-01-01')
+ 'expireDate' => new \DateTime('1971-01-01'),
+ 'sameSite' => 'Lax',
],
'bar' => [
'value' => 'expired',
- 'expireDate' => new \DateTime('1971-01-01')
+ 'expireDate' => new \DateTime('1971-01-01'),
+ 'sameSite' => 'Lax',
]
];