Delete cookies inside browser for website domain on home page

Hi,

I want to delete the cookies related to my website domain (www.mydomain.com) from the browsers (Chrome,Firefox,Safari,Opera etc) when visitor visit my website.

I tried below example but not working. I can see the cookies in the browser


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

Any idea?

Best Regards,

This should work:

foreach ($_COOKIE as $name => $value) {
  setcookie($name, '', time()-1000);
  setcookie($name, '', time()-1000, '/');
}

The problem with your approach is that it assumes that

  1. A cookie name or value will never contain ;
  2. A cookie name or value will never contain =

As soon as one if these is not true the entire thing comes crumbling down

1 Like

Thanks. It works. :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.