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

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

Gajus
  • 69,002
  • 70
  • 275
  • 438
user198729
  • 61,774
  • 108
  • 250
  • 348
  • Check this answer: http://stackoverflow.com/questions/2310558/how-to-delete-all-cookies-of-my-website-in-php/2310591#2310591 – trante Jan 04 '13 at 19:12

3 Answers3

13

This should do the trick:

foreach ($_COOKIES as $c_id => $c_value)
{
    setcookie($c_id, NULL, 1, "/", ".domain.name");
}
Michal M
  • 9,322
  • 8
  • 47
  • 63
  • Don't know how exactly you expect to set a multi-dimensional array cookie unless the id is like `test[something]` – Tyler Carter Dec 16 '09 at 02:33
  • well, yeah, since it's referring the name of the cookie, so whatever type the cookie is, it'll be cleared. – Michal M Dec 16 '09 at 06:50
  • 4
    Generally this is correct, but there is an error in your answer - it should be $_COOKIE, not $_COOKIES – Sych Aug 04 '15 at 16:06
0
    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, '/');
        }
    }
-11

Man, isn't it easier to just wipe out all cookies like this:

$_COOKIE=array();
mpen
  • 272,448
  • 266
  • 850
  • 1,236
AJ.
  • 1
  • 1