Skip to content

Commit

Permalink
Issue #153: Add support for SameSite cookie flag
Browse files Browse the repository at this point in the history
Add support for SameSite cookie flag in PHP >= 7.3.0. I split this out as a separate if statement to keep existing functionality in 7.2.x as similar as possible. When dropping support for 7.2.x, the idea would be to turn the method into just what's in side the version-comparing `if`.
  • Loading branch information
alexekorn authored Jun 21, 2020
1 parent 6b21367 commit 1e6a93e
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ protected static function getConfig(string $stored): SymmetricConfig
* @param string $domain (defaults to NULL)
* @param bool $secure (defaults to TRUE)
* @param bool $httpOnly (defaults to TRUE)
* @param string $samesite (defaults to ''; PHP >= 7.3.0)
* @return bool
*
* @throws InvalidDigestLength
Expand All @@ -156,16 +157,34 @@ public function store(
string $path = '/',
string $domain = '',
bool $secure = true,
bool $httpOnly = true
bool $httpOnly = true,
string $sameSite = ''
): bool {
$val = Crypto::encrypt(
new HiddenString(
(string) \json_encode($value)
),
$this->key
);
if (\version_compare(PHP_VERSION, '7.3.0') >= 0) {
$options = [
'expires' => (int) $expire,
'path' => (string) $path,
'domain' => (string) $domain,
'secure' => (bool) $secure,
'httponly' => (bool) $httpOnly,
];
if ($sameSite !== '') {
$options['samesite'] = (string) $sameSite;
}
return \setcookie(
$name,
$val,
$options);
}
return \setcookie(
$name,
Crypto::encrypt(
new HiddenString(
(string) \json_encode($value)
),
$this->key
),
$val,
(int) $expire,
(string) $path,
(string) $domain,
Expand Down

0 comments on commit 1e6a93e

Please sign in to comment.