Codeigniter无法创建cookie

发布于 2024-12-17 11:16:51 字数 678 浏览 0 评论 0原文

我正在尝试用 codeigniter 创建一个 cookie 大约 2 天 -.- (之前我很羞于问这个问题......)

有人愿意解释一下这段代码有什么问题吗:

    $websiteUrl = preg_replace("/^[\w]{2,6}:\/\/([\w\d\.\-]+).*$/","$1", base_url());
    $this->load->helper('cookie');

    $cookie = array(
               'name'   => 'rememberMe',
               'value'  => $this->encrypt->encode(serialize($serialize)),
               'expire' => (time() + $this->config->item('remember_me')),
               'domain' => '.'.$websiteUrl,
               'path'   => '/',
               'prefix' => 'chv_',
               'secure' => false,
           );

    set_cookie($cookie);

I'm trying to create a cookie with codeigniter for like 2 days -.- (I was to ashame to ask the question before...)

Anyone care to explain me what is wrong with this code:

    $websiteUrl = preg_replace("/^[\w]{2,6}:\/\/([\w\d\.\-]+).*$/","$1", base_url());
    $this->load->helper('cookie');

    $cookie = array(
               'name'   => 'rememberMe',
               'value'  => $this->encrypt->encode(serialize($serialize)),
               'expire' => (time() + $this->config->item('remember_me')),
               'domain' => '.'.$websiteUrl,
               'path'   => '/',
               'prefix' => 'chv_',
               'secure' => false,
           );

    set_cookie($cookie);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

焚却相思 2024-12-24 11:16:51
$this->input->set_cookie($cookie)

$this 是 CI 实例)

确保 $this->config->item('remember_me') > 0


我多次使用 CI,并且总是使用本机 setcookie() 函数,因为我真的不需要任何框架来设置 cookie(操作很简单)...但是根据使用 CI 实例和输入的文档 CI->input->set_cookie() 应该可以完成这项工作。 记住没有框架是 100% 完美工作的...这只是框架...您可以逐步调试 CI 代码以查看会发生什么。

(来自评论)

$this->input->set_cookie($cookie)

($this is CI instance)

Make sure $this->config->item('remember_me') > 0


I was working with CI many times and i was always using native setcookie() function, because i really don't need any framework to set cookie (it's simple operation)... But according to documentation using CI instance and input CI->input->set_cookie() should do the job. Remember NO FRAMEWORK is 100% perfect working... It's only framework... You can debug step-by-step CI code to see what happens.

(from comment)

回忆那么伤 2024-12-24 11:16:51

确保在运行 set_cookie 之前没有加载额外的空格,否则会阻止 cookie 标头发送到浏览器。我花了很多个小时来追踪这个问题,发现我在某个地方的结束标签末尾有一个额外的空格。如果您打开 E_WARNING,这应该会揭示问题。

Make sure that there is no extra whitespace being loaded before you are running set_cookie that is preventing the cookie header from being sent to the browser. I have spent many an hour tracking down that issue and found i had an extra space at the end of a closing tag somewhere. If you turn E_WARNING on, this should reveal the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文