Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for 'samesite' cookie option (PHP 7.3.0+) #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions src/SecureHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,29 @@ protected function getKey($name)
$key = random_bytes(64); // 32 for encryption and 32 for authentication
$cookieParam = session_get_cookie_params();
$encKey = base64_encode($key);
setcookie(
$name,
$encKey,
// if session cookie lifetime > 0 then add to current time
// otherwise leave it as zero, honoring zero's special meaning
// expire at browser close.
($cookieParam['lifetime'] > 0) ? time() + $cookieParam['lifetime'] : 0,
$cookieParam['path'],
$cookieParam['domain'],
$cookieParam['secure'],
$cookieParam['httponly']
);
// if session cookie lifetime > 0 then add to current time
// otherwise leave it as zero, honoring zero's special meaning
// expire at browser close.
$expires = ($cookieParam['lifetime'] > 0) ? time() + $cookieParam['lifetime'] : 0;

if (version_compare(PHP_VERSION, '7.3.0', '>=')) {
// PHP 7.3.0+ can use options as array,
// however session_get_cookie_params() returns 'lifetime',
// but setting the options via array requires you to use 'expires'
$cookieParam['expires'] = $expires;
unset($cookieParam['lifetime']);
setcookie($name, $encKey, $cookieParam);
} else {
setcookie(
$name,
$encKey,
$expires,
$cookieParam['path'],
$cookieParam['domain'],
$cookieParam['secure'],
$cookieParam['httponly']
);
}
$_COOKIE[$name] = $encKey;
} else {
$key = base64_decode($_COOKIE[$name]);
Expand Down