Võ Minh
Võ Minh

Reputation: 155

can not delete cookie, it still show up call it in other file

create cookie

$cookie_name = "userw";
$cookie_value = "John due";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day

if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}

delete cookie

// set the expiration date to one hour ago
$cookie_name = "userw";

setcookie("$cookie_name", "", time() - 3600);
unset($_COOKIE[$cookie_name]);


$cookie_name = "userw";
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}

read cookie after deleted

$cookie_name = "userw";
if(!isset($_COOKIE[$cookie_name])) {
    echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
    echo "Cookie '" . $cookie_name . "' is set!<br>";
    echo "Value is: " . $_COOKIE[$cookie_name];
}

If I read the cookie in the file while with unset, the cookie does not appear. but when I read the cookie again it shows up. I test it in xampp. Does anyone have any solution for this?

Upvotes: 0

Views: 16

Answers (1)

Jeto
Jeto

Reputation: 14927

When removing the cookie, you need to specifiy the same path that you specified when creating it (unless you want to do very specific things):

Try this:

setcookie($cookie_name, '', time() - 3600, '/');   // note the 4th param, '/'

Upvotes: 1

Related Questions