Home  >  Article  >  Backend Development  >  How to Effectively Purge All Website Cookies in PHP Upon User Logout?

How to Effectively Purge All Website Cookies in PHP Upon User Logout?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-24 18:32:35984browse

How to Effectively Purge All Website Cookies in PHP Upon User Logout?

Purging Website Cookies in PHP Upon User Logout

To effectively remove all cookies associated with your website when a user logs out, the setcookie function alone may not suffice as it only unsets a specific cookie. A more comprehensive solution is required to tackle this task.

PHP setcookie()

The setcookie function enables the manipulation of cookies. Referencing the official PHP documentation, this code snippet erases all cookies for your domain:

<code class="php">// unset cookies
if (isset($_SERVER['HTTP_COOKIE'])) {
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
    foreach($cookies as $cookie) {
        $parts = explode('=', $cookie);
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);
        setcookie($name, '', time()-1000, '/');
    }
}</code>

The above is the detailed content of How to Effectively Purge All Website Cookies in PHP Upon User Logout?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn