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

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

Gajus
  • 62,714
  • 68
  • 259
  • 405
user198729
  • 58,910
  • 106
  • 245
  • 345
  • 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,046
  • 6
  • 46
  • 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
  • 3
    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
  • 256,080
  • 255
  • 805
  • 1,172
AJ.
  • 1
  • 1