Cakephp cookie总是自动删除 [英] Cakephp cookie always get deleted automatically

查看:218
本文介绍了Cakephp cookie总是自动删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都有一个想法,为什么Cookie在Cakephp总是会在函数结束后自动删除?

Anybody have an idea, why cookie in Cakephp always get deleted automatically after function ends?

我尝试写一个cookie在let说函数abc,

I try to write a cookie in let say function abc, with following :

$this->Cookie->write('referal', $ref);

在此之前,在app控制器过滤器之前,我已经初始化cookie如下:

Before that, in app controller before filter, i have initialized the cookie as following :

$this->Cookie->name = 'renttycoons';
$this->Cookie->time = 604800; // or '1 week'
$this->Cookie->path = '/';
$this->Cookie->domain = 'rent.local';
$this->Cookie->key = 'qSI232qs*&sXOw!';

但是一旦函数的执行结束,cookie就为空。当我尝试读取cookie之前函数abc结束,它在那里。 。

But once the execution of function ends, the cookie was empty. when i try to read the cookie before function abc ends, it was there. There was no delete cookie method anyway.

推荐答案

是的,因为Cookie在Cake中的工作原理:当你使用Cookie-> write (),它不会直接写入cookie,因为cookie是在用户的浏览器中。只有直到视图被呈现,您写的cookie发送。所以当你重定向,(我猜猜,cookie不发送和刷新,因为视图没有呈现)新的请求有旧的Cookie数据。

yes, because the way Cookie in Cake works: when you use Cookie->write(), it doesn't directly write to the cookie, because the cookie is in the user's browser. Only until the view is rendered that the cookie you wrote is sent. So when you redirect, (I would guess the cookie doesn't get sent and flushed out because the view isn't rendered) the new request has the old cookie data.

如果您想要在cake应用程序中保留一些共享数据,并且每个访问者都是唯一的,请使用SessionComponent。它看起来与Cookie差不多: $ this-> Session-> write('referal',$ ref); $ this - > Session-> read('referal');

If you want to persist some shared data within cake app, and unique to each visitor, use SessionComponent. It looks pretty much the same as Cookie: $this->Session->write('referal', $ref); and $this->Session->read('referal');

这篇关于Cakephp cookie总是自动删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆