设置cookie在CodeIgniter中不起作用

前端之家收集整理的这篇文章主要介绍了设置cookie在CodeIgniter中不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想设置一个名为csrf_cookie_name的cookie,其中包含此函数的值$this-> security-> get_csrf_hash();但是,它不起作用.

我在我的控制器中有这个:

  1. $csrf_cookie_value = $this->security->get_csrf_hash();
  2. $this->input->set_cookie('csrf_cookie_name',$csrf_cookie_value);
  3. echo $this->input->cookie('csrf_cookie_name');
  4. die();

但它不起作用,没有任何回应.

如果我只尝试这个:

  1. $csrf_cookie_value = $this->security->get_csrf_hash();
  2. echo $csrf_cookie_value;

我工作,生成的字符串被回显.

所以,我认为接下来的两行中的某些内容错误的:

  1. $this->input->set_cookie('csrf_cookie_name',$csrf_cookie_value);
  2. echo $this->input->cookie('csrf_cookie_name');

谢谢你的建议.

解决方法

您需要指定cookie的生命周期. 0将是一个会话cookie,其他任何东西都将被添加到time().

如果您未指定生命周期,CI将解释您要删除cookie.而这正是它的作用:)

  1. $this->input->set_cookie('name','value',0); //expires when the browser window closes
  2. $this->input->set_cookie('name',3600); //expires in one hour
  3. $this->input->set_cookie('name','value'); //will delete the cookie (if the cookie does not exist,you will not notice anything happening)

猜你在找的HTML相关文章