我想设置一个名为csrf_cookie_name的cookie,其中包含此函数的值$this-> security-> get_csrf_hash();但是,它不起作用.
我在我的控制器中有这个:
- $csrf_cookie_value = $this->security->get_csrf_hash();
- $this->input->set_cookie('csrf_cookie_name',$csrf_cookie_value);
- echo $this->input->cookie('csrf_cookie_name');
- die();
但它不起作用,没有任何回应.
如果我只尝试这个:
- $csrf_cookie_value = $this->security->get_csrf_hash();
- echo $csrf_cookie_value;
我工作,生成的字符串被回显.
- $this->input->set_cookie('csrf_cookie_name',$csrf_cookie_value);
- echo $this->input->cookie('csrf_cookie_name');
谢谢你的建议.
解决方法
您需要指定cookie的生命周期. 0将是一个会话cookie,其他任何东西都将被添加到time().
如果您未指定生命周期,CI将解释您要删除cookie.而这正是它的作用:)
- $this->input->set_cookie('name','value',0); //expires when the browser window closes
- $this->input->set_cookie('name',3600); //expires in one hour
- $this->input->set_cookie('name','value'); //will delete the cookie (if the cookie does not exist,you will not notice anything happening)