Codeigniter无法创建cookie
我正在尝试用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(
$this
是 CI 实例)确保
$this->config->item('remember_me')
> 0我多次使用 CI,并且总是使用本机 setcookie() 函数,因为我真的不需要任何框架来设置 cookie(操作很简单)...但是根据使用 CI 实例和输入的文档
CI->input->set_cookie()
应该可以完成这项工作。 记住没有框架是 100% 完美工作的...这只是框架...您可以逐步调试 CI 代码以查看会发生什么。(来自评论)
(
$this
is CI instance)Make sure
$this->config->item('remember_me')
> 0I 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)
确保在运行 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.