Home >Backend Development >PHP Tutorial >How to Completely Delete a Cookie in PHP?
How to Effectively Remove a Cookie in PHP
In PHP, removing a cookie solely through the unset() function may not always be effective in completely removing it. To ensure a proper removal, it is recommended to combine the unset() function with an additional setcookie() call to overwrite the cookie with an expiration timestamp in the past.
To illustrate:
if (isset($_COOKIE['remember_user'])) { unset($_COOKIE['remember_user']); // Remove the cookie from the $_COOKIE array setcookie('remember_user', '', -1, '/'); // Set the cookie with an expiration timestamp in the past return true; } else { return false; }
In this example:
Note that the setcookie() function takes four arguments:
By following this method, you can reliably remove a cookie from both the PHP environment and the client's browser.
The above is the detailed content of How to Completely Delete a Cookie in PHP?. For more information, please follow other related articles on the PHP Chinese website!