无法在Codeigniter的内部结构中设置Cookie [英] Can't set cookie inside construct in codeigniter
问题描述
如果 null
,我试图设置一个cookie,但我无法使其正常工作:
I trying to set a cookie if null
but I can't get it to work:
public function __construct()
{
parent::__construct();
if ($this->input->cookie('ff', TRUE) == FALSE)
{
$this->input->set_cookie('ff', 'on', 86500);
dump($this->input->cookie('ff', TRUE));
}
}
我在做什么错了?
dump()
这只是一个自定义调试功能.
dump()
it's just a custom debug function.
推荐答案
set_cookie调用可能失败的几个原因:
A couple of reasons your set_cookie call might fail:
1)调用set_cookie时,您已经向浏览器输出了某些内容,在这种情况下,您可能会在已发送标头"的行中收到错误/通知.要解决此问题,只需确保在发送任何内容到浏览器之前发生set_cookie调用即可.
1) You've output something to the browser already when set_cookie is called, in which case you might get an error/notice along the lines of "Headers already sent". To fix that, just ensure that the set_cookie call occurs before sending anything to the browser.
2)如果您正在进行本地开发和测试,则"localhost"不被视为有效域,因此不会为它保存任何cookie.一种解决方法是在您的主机文件中添加一个条目,例如:
2) If you're doing local development and testing, "localhost" is not considered a valid domain, so no cookies will be saved for it. A workaround would be to add an entry in your hosts file, for example:
localhost.dev 127.0.0.1
然后使用该域而不是"localhost"进行测试.
And then test with that domain, instead of "localhost".
这篇关于无法在Codeigniter的内部结构中设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!