2013-03-18 128 views
0

我設置一個cookie如下:PHP刪除Cookie問題

if($persist=='persist'){ // if remember me then set long cookie expiration 

     setcookie("aukCookie", "$user", time()+9900000, "/", "myurl.com"); 
     setcookie("aukCookie2", "$username", time()+9900000, "/", "myurl.com"); 
     setcookie("AUKpersist", "$user", 0, "/", "myurl.com"); 

    } else { 

     setcookie("aukCookie", "$user", time()+86400, "/", "myurl.com"); 
     setcookie("aukCookie2", "$username", time()+9900000, "/", "myurl.com"); 

    } 

然後刪除我去同一個文件,url變量?logoff=true

然後我嘗試以下方法:

if ($_GET[logoff]=='true'){ 
    setcookie ("aukCookie", "", time()-3600, "/", "myurl.com"); 
    setcookie ("AUKpersist", "", time()-3600, "/", "myurl.com"); 
    // use below to send user somewhere when logged out successfully 
    header("Location: /userAdmin/userlogin.html?loggedOut"); 
} 

但是這個cookie根本就沒有被刪除。 header()函數工作正常。

任何想法?有一個更好的方法嗎?

+0

是'header'頁面發送數據之前叫這就是你需要使用'cookie'也許'session' – Jacopofar 2013-03-18 12:12:04

+0

,感謝 – mkjasinski 2013-03-18 12:12:17

回答

2

嘗試設置爲空,而不是空的,如:

if ($_GET[logoff]=='true'){ 
    setcookie ("aukCookie", NULL, time()-3600, "/", "myurl.com"); 
    setcookie ("AUKpersist", NULL, time()-3600, "/", "myurl.com"); 
    // use below to send user somewhere when logged out successfully 
    header("Location: /userAdmin/userlogin.html?loggedOut"); 
} 
+0

試過沒有工作的最可能的解釋 – 2013-03-18 12:37:35

+0

是嗎??確定這是否條件正在工作?我的意思是$ _GET [註銷]返回true? – periback2 2013-03-18 13:18:40

+0

@DarrenSweeney ?? – periback2 2013-03-18 13:50:43

1

unset

即嘗試,

unset($_COOKIE["aukCookie"]); 

它將工作

0

嘗試

setcookie("aukCookie", "", -1, "/", "myurl.com"); 
-1

刪除Cookie

布爾的setcookie(字符串$名稱[,字符串$價值[摘要$到期= 0,字符串$路徑[,字符串$域[,布爾$安全=假[,布爾$僅Http =假]]]]]])

setcookie ("mycookiename", "", time() - 3600);

另一示例

setcookie("mycookiename", $value, time()+3600, "/~rasmus/", "example.com", 1);

測試它

enter code here // Print an individual cookie 

enter code here echo $_COOKIE["mycookiename"]; echo $HTTP_COOKIE_VARS["mycookiename"];

// Another way to debug/test is to view all cookies print_r($_COOKIE); 這是一組Cookie概念

註銷的情況下,你需要取消設置餅乾像下面的例子

unset($_COOKIE['mycookiename']);

像這樣

如果($ _GET [註銷] == '真'){

未設置($ _ COOKIE [ 'aukCookie']);

unset($ _ COOKIE ['AUKpersist']);

header(「Location:/userAdmin/userlogin.html?loggedOut「);

}