PHP删除cookie问题

时间:2013-03-18 12:09:44

标签: php cookies

我按如下方式设置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()函数工作正常。

有什么想法吗?有更好的方法吗?

4 个答案:

答案 0 :(得分:2)

尝试设置null而不是空,例如:

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"); 
}

答案 1 :(得分:1)

请尝试使用unset

即,

unset($_COOKIE["aukCookie"]);

它会起作用

答案 2 :(得分:0)

尝试

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

答案 3 :(得分:-1)

删除Cookie

bool setcookie(string $ name [,string $ value [,int $ expire = 0 [,string $ path [,string $ domain [,bool $ secure = false [,bool $ httponly = false]] ]]]])

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概念

如果 logOff ,您需要取消设置 Cookie,例如以下示例

unset($_COOKIE['mycookiename']);

像这样的东西

if($ _GET [logoff] ==' true'){

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

未设置($ _ COOKIE [' AUKpersist']);

标题("位置:/userAdmin/userlogin.html?loggedOut");

}