简体   繁体   中英

PHP delete cookie issue

I am setting a cookie as follows:

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

    }

Then to delete I go to the same file with url variable ?logoff=true

Then I try the following:

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

But the cookie is simply not being deleted. The header() function works fine.

Any ideas? Is there a better way to do this?

try setting null instead of empty, like:

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

Please try with unset

ie,

unset($_COOKIE["aukCookie"]);

It will work

尝试

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

To delete cookies

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

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

another example

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

Test it

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); this is the set cookies concept

In case of logOff you need to unset the cookies like below example

unset($_COOKIE['mycookiename']);

Something like this

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

unset($_COOKIE['aukCookie']);

unset($_COOKIE['AUKpersist']);

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM