简体   繁体   English

codigneter setcookie在我的情况下不起作用

[英]codigneter setcookie doesn't work in my case

if(isset($_COOKIE['fb_userId'])){
    setcookie('fb_userId', $userId);
}

I put this within one of my controller, the cookie named userId should be inserted after the call right? 我将其放在我的一个控制器中,应该在调用之后插入名为userId的cookie吗? but I checked the resources tab of my chrome, I don't see anything.. 但我检查了Chrome的资源标签,但看不到任何内容。

I also tried the helper method like this 我也尝试过这样的辅助方法

$this->input->set_cookie($cookie_arr);

In your code above, it will only set the cookie if the same cookie name (fb_userId) already exists. 在上面的代码中,仅当已经存在相同的cookie名称(fb_userId)时 ,它才会设置cookie。

Try use the following code: 尝试使用以下代码:

if(!isset($_COOKIE['fb_userId'])){
   setcookie('fb_userId', $userId);
}

I've add the ! 我添加了 before your isset() isset()之前

if you want to set cookie in Codeigniter than you can use that code 如果您想在Codeigniter中设置cookie,则可以使用该代码

 $this->load->helper('cookie');
     $cookie = array(
       'name'   => 'testcookie',
       'value'  => 'test value',
       'expire' => '86500'
      );

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

     echo $_COOKIE['testcookie'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM