php讀取不已cookie

來源:互聯網
上載者:User
php讀取不了cookie
做一個登入介面,自動記憶使用者名稱的功能

if(empty($_POST['chkRemember'])) {
//使用者沒有選擇單選框
if(!empty($_COOKIE['username'])){
setcookie('username','',time()-100);
}
}else{
//使用者選擇單選框
setcookie('username',$username,time()+7*24*3600);
}

當使用者選了記住使用者名稱的單選框時,COOKIE已經儲存,在C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Cookies菜單下多了一個COOKIE檔案(administrator@localhost[1].txt),內容也是正確的

但是讀取COOKIE卻不成功,每次都是返回控制


function GetCookieVal($key){
if(empty($_COOKIE[$key])){
return "none";
}else{
return $_COOKIE[$key];
}
}
?>

直接列印COOKIE數組 print_r($_COOKIE);
得到的也是空數組


求教這是怎麼回事?
是不是COOKIE儲存路徑和讀取路徑不一致?

分享到:


------解決方案--------------------
1、
setcookie('username',$username,time()+7*24*3600);

GetCookieVal("username")
必須是在不同的 HTTP 會話中進行的

2、你在設定 cookie 時 沒有指定路徑
所以 cookie 只在執行 setcookie 的程式所在路徑下有效
如果執行 GetCookieVal 的程式不在同一路徑下,則不會取到值

setcookie('username',$username,time()+7*24*3600, '/');
這樣才可以使 cookie 變數 username 在整個網站中有效
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.