Folks,
I set cookie. Then echo it.
Then I delete cookie and check if cookie gets echoed or not any further .Shouldn’t get echoed since I deleted it. But, cookie gets echoed after deletion. Why?
““
<!DOCTYPE html>
<?php
$cookie_name = “user”;
$cookie_value = “John Doe”;
setcookie(“$cookie_name”,”$cookie_value”,time()+1*24*60*60);
if(!isset($_COOKIE[$cookie_name])) {
echo “Step 1: Cookie named ‘” . $cookie_name . “‘ is not set!<br>”;
} else {
echo “Cookie ‘” . $cookie_name . “‘ is set!<br>”;
echo “Value is: ” . $_COOKIE[$cookie_name] . “<br><br>”;
}
//Delete Cookie now
setcookie(“$cookie_name”, “”, time() – 3600);
echo “Step 2: Cookie ‘user’ is deleted.<br>”;
//Test if Cookie is deleted or not.
if(isset($_COOKIE[$cookie_name])) {
echo “Cookie named ‘” . $cookie_name . “‘ is still set! Cookie still not deleted yet! Why ?<br>”;
echo “Value is: ” . $_COOKIE[$cookie_name] . “<br><br>”;
}
?>
<p><strong>Note:</strong> You might have to reload the page to see the value of the cookie.</p>
</body>
</html>
I get echoed the following no matter how many times I reload/refresh/view the page.
**Step 1b: Cookie ‘user’ is set!
Value is: John Doe
Step 2a: Cookie ‘user’ is deleted.
Step 2b: Cookie named ‘user’ is still set! Cookie still not deleted yet! Why ?
Value is: John Doe
Note: You might have to reload the page to see the value of the cookie.**