/    Sign up×
Community /Pin to ProfileBookmark

Cookie Deletion Fails – Cookie Still Set!

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.**

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorOct 22.2020 — @DaveyErwin

Do you mind checking PM ?
Copy linkTweet thisAlerts:
@developer_webauthorOct 22.2020 — @Nachfolger

I know you hate me mentioning you like this but ....

Man,

What can I do without yaa!
Copy linkTweet thisAlerts:
@NachfolgerOct 22.2020 — The cookie is still set within the variable space until the page is reloaded. There are easy ways to circumvent that, but you can use google and find the answer on your own.

I have told you before, and I will say it again. If I don't post on a thread, it's because I have no desire to do so. Please respect that and **DO NOT TAG ME** on a thread I have not posted in.
Copy linkTweet thisAlerts:
@NogDogOct 22.2020 — One final time: setcookie() does not set -- or delete -- anything in $_COOKIE. All it does is create a cookie to be sent to the client. It's the receipt of any cookie back from the client on subsequent requests that determines what is in the $_COOKIE array.
Copy linkTweet thisAlerts:
@waliddOct 24.2020 — The cookie is still set within the variable space until the page is reloaded. There are easy ways to circumvent that, but you can use google and find the answer on your own.

I have told you before, and I will say it again. If I don't post on a thread, it's because I have no desire to do so. Please respect that and DO NOT TAG ME on a thread I have not posted in.
Copy linkTweet thisAlerts:
@developer_webauthorOct 31.2020 — @Nachfolger#1624519

Good thing I reading this cos I just tagged you on another thread, even though you told me not to. Then, I changed my mind. Thought better not tresspass. And, tagged NogDog instead. His position is different than your's since he is a patient mod.
Copy linkTweet thisAlerts:
@developer_webauthorOct 31.2020 — @NogDog#1624520

Problem got sorted after I put setcookie() above the <html> tag.
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...