Home >Backend Development >PHP Tutorial >Why Doesn't `unset($_COOKIE['cookie_name'])` Remove a Cookie, and How Can I Do It Properly?
How to effectively remove a cookie
When attempting to remove a cookie using unset($_COOKIE['hello']);, you may notice that the cookie remains visible in your browser's cookie manager. This is because the unset function only removes the cookie from PHP's internal memory, not from the browser.
To permanently remove a cookie, the following steps are necessary:
Unset the cookie from PHP's internal memory:
unset($_COOKIE['remember_user']);
Set the cookie to expire in the past (e.g., 1 hour ago):
setcookie('remember_user', '', time() - 3600, '/');
Return a success message (optional):
return true;
By following these steps, you can effectively remove a cookie from the browser and prevent it from being used in future requests.
The above is the detailed content of Why Doesn't `unset($_COOKIE['cookie_name'])` Remove a Cookie, and How Can I Do It Properly?. For more information, please follow other related articles on the PHP Chinese website!