gpt4 book ai didi

cookies - 拉拉维尔 5.2 : retrieving a cookie via blade returns null also if cookie is set

转载 作者:行者123 更新时间:2023-12-02 18:54:54 24 4
gpt4 key购买 nike

我通过 Javascript 设置了 cookie my_cookie

   function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
expires = "; expires="+date.toUTCString();
}
else {
expires = "";
}
document.cookie = name+"="+value+expires+"; path=/";
}

....
createCookie('my_cookie', 1, 365);
....

通过 Chrome Cookie Inspector,我看到 cookie 是用值 1 创建的。

通过 Laravel Blade 我尝试过:

 @if (Cookie::get('my_cookie') !== null) // or Cookie::get('my_cookie') == 1 or Cookie::get('my_cookie') == '1'
<p>set</p>
@else
<p>unset</p>
@endif

写入unset

 @if (request()->cookie('my_cookie') == '1') // or @if (request()->cookie('my_cookie') == 1)
<p>set</p>
@else
<p>unset</p>
@endif

结果始终是未设置

最佳答案

您可以使用

App\Http\Middleware\EncryptCookies 的字段 $ except 满足您的需求。

中间件的代码如下所示

<?php

namespace App\Http\Middleware;

use Illuminate\Cookie\Middleware\EncryptCookies as BaseEncrypter;

class EncryptCookies extends BaseEncrypter
{
/**
* The names of the cookies that should not be encrypted.
*
* @var array
*/
protected $except = [
'my_cookie'
];
}

现在您可以使用 Cookie::get('my_cookie')request()->cookie('my_cookie') 来检索它

关于cookies - 拉拉维尔 5.2 : retrieving a cookie via blade returns null also if cookie is set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39052612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com