如何在 Laravel 中对 cookie 设置安全标志



我想在通过HTTPS访问内容时为cookie数据设置安全标志。

Cookie 用于整个应用程序,因此需要全局配置以保护所有 Cookie。

您需要

使用 session_set_cookie_params 覆盖默认设置,将$secure标志设置为 true,

void session_set_cookie_params ( int $lifetime [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]] )

更多信息请访问 php.net

在 laravel 中您需要更改配置/会话.php配置,将安全标志设置为 true

/*
|--------------------------------------------------------------------------
| HTTPS Only Cookies
|--------------------------------------------------------------------------
|
| By setting this option to true, session cookies will only be sent back
| to the server if the browser has a HTTPS connection. This will keep
| the cookie from being sent to you if it can not be done securely.
|
*/
'secure' => true,

在较新版本的 laravel 中,您只需在.env文件中设置SESSION_SECURE_COOKIE=true即可

您可以在

config/session.php文件中设置secure true的值,如下图所示;

'secure' => env( 'SESSION_SECURE_COOKIE', true ),

最新更新