Can't set cookie inside construct in codeigniter

谁说胖子不能爱 提交于 2019-12-13 04:05:55

问题


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));
    }
}

What am I doing wrong?

Edit: dump() it's just a custom debug function.


回答1:


A couple of reasons your set_cookie call might fail:

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) 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

And then test with that domain, instead of "localhost".



来源:https://stackoverflow.com/questions/9069521/cant-set-cookie-inside-construct-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!