如何在 Laravel 中的 Cookie 上设置安全标志

问题描述 投票:0回答:3

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

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

php security cookies laravel-5
3个回答
41
投票

您需要使用

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 中你需要更改 config/session.php 配置,将 secure 标志设置为 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 中,您只需在

SESSION_SECURE_COOKIE=true
文件中设置
.env


36
投票

您可以在

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

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

-1
投票

您可以通过会话文件中的一些更改来修复此问题,请参阅此处 https://www.appetenza.com/how-to-fix-cookie-does-not-contain-the-secure-and-httponly-attribute-in -laravel

© www.soinside.com 2019 - 2024. All rights reserved.