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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?