How to delete all cookies in PHP?

setcookie('id', null, 1, "/", ".domain.name");

The above will only delete a specific cookie, but how to delete all of them?

+3
source share
3 answers

This should do the trick:

foreach ($_COOKIES as $c_id => $c_value)
{
    setcookie($c_id, NULL, 1, "/", ".domain.name");
}
+13
source
    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, '/');
        }
    }
0
source

Man, isn’t it easier to destroy all cookies like this:

$_COOKIE=array();
-10
source

Source: https://habr.com/ru/post/1725793/


All Articles