Home > Backend Development > PHP Tutorial > 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
Release: 2024-10-24 18:32:35
Original
1080 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template