Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a cookie

Tags:

php

cookies

People also ask

What does removing a cookie do?

When you delete cookies from your computer, you erase information saved in your browser, including your account passwords, website preferences, and settings. Deleting your cookies can be helpful if you share your computer or device with other people and don't want them to see your browsing history.

Can I delete a specific cookie in Chrome?

Follow these steps to remove a specific cookie from Chrome on your desktop or laptop computer. Open Chrome. Go to Settings > Privacy and Security> Site Settings > Cookies and site data > See all cookies and site data. Find the cookie you want to delete and click the “Trashcan” icon to its right.

Is removing cookies a good idea?

You definitely should not accept cookies – and delete them if you mistakenly do. Outdated cookies. If a website page has been updated, the cached data in cookies might conflict with the new site. This could give you trouble the next time you try to upload that page.

Can I delete one cookie?

To delete an individual cookie, select the cookie in the list, and click “Remove Selected”. To delete all cookies for a specific website, select the website folder and click “Remove Selected”.


You May Try this

if (isset($_COOKIE['remember_user'])) {
    unset($_COOKIE['remember_user']); 
    setcookie('remember_user', null, -1, '/'); 
    return true;
} else {
    return false;
}

Set the value to "" and the expiry date to yesterday (or any date in the past)

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

Then the cookie will expire the next time the page loads.


A clean way to delete a cookie is to clear both of $_COOKIE value and browser cookie file :

if (isset($_COOKIE['key'])) {
    unset($_COOKIE['key']);
    setcookie('key', '', time() - 3600, '/'); // empty value and old timestamp
}

To reliably delete a cookie it's not enough to set it to expire anytime in the past, as computed by your PHP server. This is because client computers can and often do have times which differ from that of your server.

The best practice is to overwrite the current cookie with a blank cookie which expires one second in the future after the epoch (1 January 1970 00:00:00 UTC), as so:

setcookie("hello", "", 1);

That will unset the cookie in your code, but since the $_COOKIE variable is refreshed on each request, it'll just come back on the next page request.

To actually get rid of the cookie, set the expiration date in the past:

// set the expiration date to one hour ago
setcookie("hello", "", time()-3600);