我正在设置一个网页,使用 cookie 来确定用户是否已经登录,使用包含他的 id 的 cookie。问题是:cookie 未写入或 cookie 集合未更新。
我试过阅读文档,但它没有定义 CookieCollection 的用法。
这是我编写 cookie 的函数:
function displayData(){
$id = $this->getRequest()->getSession()->read('id');
$cookies = CookieCollection::createFromServerRequest($this->getRequest());
if(!$cookies->has('id')){
$cookie = (new Cookie('id'))
->withValue($id)
->withExpiry(new DateTime('+999 year'))
->withPath('/')
->withDomain('break-first.eu')
->withSecure(true)
->withHttpOnly(true);
$cookies = $cookies->add($cookie);
}
// Other stuff
}
在我尝试阅读的地方:
function index(){
$cookies = $this->getRequest()->getCookieCollection();
dd($cookies);
}
我希望有一个名为“id”的 cookie,但我没有。只有 CAKEPHP 和 pll_language 出现。