【发布时间】:2018-11-11 02:02:00
【问题描述】:
首先,我已经尝试了这两个链接中提供的这两种解决方案
How to set and get Cookies in Cakephp 3.5
How to create cookies at controller level in CakePHP 3.5?
但是,它只是不工作。我提供了一个示例,说明我如何尝试编写和读取 cookie。但它们都不起作用。
写入 Cookie
use Cake\Http\Cookie\CookieCollection;
use Cake\Http\Cookie\Cookie;
public function writeCookie() {
$cookie = new Cookie(
'remember_me', // name
1, // value
(Time::now())->modify('+1 year'), // expiration time, if applicable
'/', // path, if applicable
'', // domain, if applicable
false, // secure only?
true // http only ?
);
$cookies = new CookieCollection([$cookie]);//To create new collection
$cookies = $cookies->add($cookie);//to add in existing collection
$this->response = $this->response->withCookie('remember_me', [
'value' => 'yes',
'path' => '/',
'httpOnly' => true,
'secure' => false,
'expire' => strtotime('+1 year')
]);
}
读取 Cookie
public function readCookie(){
$cookie = $this->request->getCookie('remember_me');
debug($cookie); //is getting a null value
}
有人能指出在 CakePHP 3.5 中写入和读取 cookie 的正确方向吗?
【问题讨论】:
-
如果看起来您实际上没有在响应中的任何位置设置 cookie,则按照 the manual。
-
嗨,格雷格,即使我使用上面链接中提供的一种方法进行了设置。我仍然无法使用 $this->request 从其他页面访问 cookie。
-
然后你需要做一些调试,首先在你的浏览器(或者你正在使用的任何客户端)中检查 HTTP 请求和响应,以检查响应中是否发送了 cookie 标头,以及是否cookie 正在与下一个请求一起发送。
-
在尝试设置 cookie 之前您是否有可能生成一些输出?这将导致它在添加 cookie 之前发送标头,并且一旦发送标头,就没有回头路了。
标签: cakephp cookies cakephp-3.0 cakephp-3.5