设置Cookie在CodeIgniter中不起作用 [英] Setting cookies not working in CodeIgniter

查看:503
本文介绍了设置Cookie在CodeIgniter中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用名为 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;

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


$ b b

所以,我假设下面两行中的内容是错误的:

  $ 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); // expire当浏览器窗口关闭
$ this-> input-> set_cookie('name','value',3600); // expires in one hour
$ this-> input-> set_cookie('name','value'); //将删除cookie(如果cookie不存在,则不会发生任何事情)


I wnat to set cookie with a name csrf_cookie_name with a value from this function $this->security->get_csrf_hash(); but, it is not working.

I have this in my controller:

   $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();

But it is not working and nothing is echoed out.

If I try only this:

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

I works and a generated string is echoed out.

So, I assume that something within these next 2 lines is wrong:

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

Thanks for your advice.

解决方案

You need to specify a life time for the cookie. 0 will be a session cookie and anything else will be added to time().

If you don't specify a life time, CI will interpret that you want to delete the cookie. And that's exactly what it does :)

$this->input->set_cookie('name', 'value', 0);    //expires when the browser window closes
$this->input->set_cookie('name', 'value', 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)

这篇关于设置Cookie在CodeIgniter中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆