Alexey
Alexey

Reputation: 337

How to delete all cookies with the same name?

I got 3 cookies with the same name on my website. http://clip2net.com/s/jm4CcZ They got different paths ('/', '/call', '/call/login') and different domains ('.domain.com', 'domain.com'). Now I use several setcookie() instuctions to delete each of them.

Is there more smart way to delete them at once?

Upvotes: 0

Views: 2924

Answers (1)

S.Pols
S.Pols

Reputation: 3434

No not all at once. If you want remove the specific cookies you need to use the cookie name. If you want to unset all cookies you can use this:

// 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, '/');
    }
}

This is a function that is published on http://php.net/manual/en/function.setcookie.php#Hcom73484

Upvotes: 1

Related Questions