不能在codeigniter中的构造中设置cookie [英] Can't set cookie inside construct in codeigniter

查看:89
本文介绍了不能在codeigniter中的构造中设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试设置一个cookie,如果 null 但我不能让它工作:

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。解决方法是在您的hosts文件中添加一个条目,例如:

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屋!

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