duanjiao4763
2013-07-01 07:08
浏览 66
已采纳

无法在codeigniter中检索cookie

I am trying to have a login function in codeigniter model, but cannot retrieve cookies there.

It returns bool false.

I have tried searching for the problem,one of the problem was cookies were not being set, but my cookies are being set in the browser, under domain localhost. And I have tried using cookies on localhost earlier in a different project, without codeigniter, in which I was able to retrieve cookies on localhost.

Here $_COOKIE['username'] would throw error invalid index 'username', $this->input->cookie('username') would return bool(false). But I can see 'username' cookie in my browser.

Cookies set and retrieved are on different files.

public function checkLogin(){

    $this->load->helper('cookie');

    $row['user']    = $this->input->cookie('username', TRUE);
    $row['handle'] = $this->input->cookie('email', TRUE);
    $row['id']      = $this->input->cookie('pid', TRUE);
    $row['type']    = $this->input->cookie('type', TRUE);
    $row['slug']    = $this->input->cookie('slug', TRUE);
    $row['cuid']    = $this->input->cookie('cuid', TRUE);

    return  $this->input->cookie('username', false);
    }
}

I set the cookies in a different file,

setcookie("username",$user, time()+60*60*60*24*30);
setcookie("email",$handle, time()+60*60*60*24*30);
setcookie("pid",$row['id'], time()+60*60*24*30*60);
setcookie("type",$type, time()+60*60*24*30*60);
setcookie("slug",$row['slug'], time()+60*60*24*30*60);
setcookie("city",$row['city'], time()+60*60*24*60*30);
setcookie("cuid", $code, time()+60*60*24*60*30);

Please ask for more information needed. I have used construct function(two underscores) in my controller, not in model. The form where cookies are being set is not in controller or model but in root folder. And I have tried setting cookies using $this->input->set_cookie(); too.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongque1462 2013-07-01 09:43
    已采纳

    Localhost may set path according to files that set the cookie.

    Declare path explicitly.

    setcookie("username",$user, time()+60*60*60*24*30, "/");
    

    Cookies with path "/" can be retrieved anywhere, otherwise not.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题