我一直在尝试这样 -
// ================
// = set a Cookie for the users city =
// ================
function set($cityId = null){
$this->components[] = 'RequestHandler';
$this->components[] = 'Cookie';
$this->Cookie->name = 'Exampleoffers';
//$this->Cookie->time = 3600; // or '1 hour'
//$this->Cookie->path = '/bakers/preferences/';
$this->Cookie->domain = 'www.example.co.uk';
$this->Cookie->secure = false;
$this->Cookie->key = 'kkjdDqSI232qs*&sXOw!';
$cities = ($this->City->find('list'));
if($cities[$cityId]) {
$this->Cookie->write ("Deal.city_id", (int) $cityId, false, "+2 months");
} else {
$this->Cookie->write ("Deal.city_id", key($cities), false, "+2 months");
}
然而,我不确定它是否与我的Authsome cookie(?)或其他东西发生冲突,但我无法读回这个值。
有没有办法在CakePHP中指定要读取哪个cookie()?
有没有办法让cookie具有2个不同的到期时间值? - 即,一个cookie的User.id的有效期为1周,而Deal.city_id的有效期是2个月,比如说?或者我认为我需要2个饼干是对的吗?
非常感谢任何提示。这是蛋糕1.3 btw!
答案 0 :(得分:1)
你可以记住,cookie会保存在系统中,所以如果你只在一个系统上保存一次cookie就会有设定值,但是,你不能拥有2个同名设置的cookie,这意味着当你去保存您必须执行此操作的Cookie:
$this->Cookie->write('Name1', $data, false, $time);
$this->Cookie->write('Name2', $data, false, $time);
如果不这样做,就会覆盖另一个。
编辑:添加一些链接,以防您有更多疑问:
CookieComponent的API页面:http://api13.cakephp.org/class/cookie-component
CookieComponent的食谱页面:http://book.cakephp.org/view/1280/Cookies