Cookie配置支持samesite配置

This commit is contained in:
Karson
2025-03-12 15:19:49 +08:00
parent d44c923b12
commit a311cedbda

View File

@@ -45,7 +45,7 @@ class Cookie
self::$config = array_merge(self::$config, array_change_key_case($config)); self::$config = array_merge(self::$config, array_change_key_case($config));
if (!empty(self::$config['httponly'])) { if (!empty(self::$config['httponly']) && PHP_SESSION_ACTIVE != session_status()) {
ini_set('session.cookie_httponly', 1); ini_set('session.cookie_httponly', 1);
} }
@@ -101,14 +101,26 @@ class Cookie
} }
$expire = !empty($config['expire']) ? $expire = !empty($config['expire']) ?
$_SERVER['REQUEST_TIME'] + intval($config['expire']) : $_SERVER['REQUEST_TIME'] + intval($config['expire']) :
0; 0;
if ($config['setcookie']) { if ($config['setcookie']) {
setcookie( if (PHP_VERSION_ID >= 70300) {
$name, $value, $expire, $config['path'], $config['domain'], // PHP 7.3+ 支持选项数组
$config['secure'], $config['httponly'] setcookie($name, $value, array_merge([
); 'expires' => $expire,
'path' => $config['path'],
'domain' => $config['domain'],
'secure' => $config['secure'],
'httponly' => $config['httponly']
], isset($config['samesite']) ? ['samesite' => $config['samesite']] : []));
} else {
// 旧版本 PHP 使用传统参数方式
setcookie(
$name, $value, $expire, $config['path'], $config['domain'],
$config['secure'], $config['httponly']
);
}
} }
$_COOKIE[$name] = $value; $_COOKIE[$name] = $value;