0

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?

1

1 Answer 1

1

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

4
  • Thanks. Why the cookies with the same name are not being rewrited?
    – Alexey
    Nov 26, 2014 at 10:11
  • You said the cookies are on a different domain. Cookies are domain related so you can have same cookie names for different (sub)domains. Note that my solution works for the cookies of one domain and cannot delete cookies of multiple domains (for the reason that cookies are domain related)
    – S.Pols
    Nov 26, 2014 at 10:12
  • yes I know about policy, but '.domain.com' & 'domain.com' are the same domains, isn't it?
    – Alexey
    Nov 26, 2014 at 10:15
  • No, '.domain.com' is an subdomain of 'domain.com'. So if you create an cookie on the subdomain '.domain.com' it will be only available on that specific subdomain.
    – S.Pols
    Nov 26, 2014 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.