出于某种原因,我无法从其他控制器读取我的cookie。代码非常简单,不确定它有什么问题。它声称cookie是空的......
PagesController:
public $components = array('Cookie');
public function beforeFilter() {
parent::beforeFilter();
//cookie setings
$this->Cookie->name = 'cookie_test';
$this->Cookie->time = 3600; // or '1 hour'
$this->Cookie->path = '/';
$this->Cookie->domain = 'localhost';
$this->Cookie->secure = false;
$this->Cookie->key = '39lbkutg1i2l0kta6785d8qki5';
$this->Cookie->httpOnly = true;
}
$this->Cookie->write('ad_id', $ad_id, false, time()+2592000);
NamesearchController中的:
$variable = $this->Cookie->read('ad_id');
答案 0 :(得分:0)
您创建了一个名为cookie_test
的Cookie,并添加了一个名为ad_id
的密钥。如果你查看你的cookies(页面信息或Firebug),你会看到一个名为cookie_test[ad_id]
的人。
Cookie组件中方法write()
和read()
的第一个参数是$key
,而不是Cookie的名称。
所以,如果你想阅读你创建的cookie,你必须这样做:
$this->Cookie->name = 'cookie_test';
$variable = $this->Cookie->read('ad_id');
答案 1 :(得分:0)
您应该在AppController中编写以下行,而不是PagesController。
public $components = array('Cookie');
public function beforeFilter() {
parent::beforeFilter();
//cookie setings
$this->Cookie->name = 'cookie_test';
$this->Cookie->time = 3600; // or '1 hour'
$this->Cookie->path = '/';
$this->Cookie->domain = 'localhost';
$this->Cookie->secure = false;
$this->Cookie->key = '39lbkutg1i2l0kta6785d8qki5';
$this->Cookie->httpOnly = true;
}