2

Ideally, to delete cookie in php, one should set exactly the same parameters when the cookie was created, except value and expire time:

Creating cookie:

setcookie('cookie1', 'value1', time()+10000, '/', 'domain.com', false, true);

Delete cookie:

setcookie('cookie1', '', time()-10000, '/', 'domain.com', false, true);

But, how can I get parameters that was set creating cookie when I want to delete it? assuming that I'm not tracking these params, for example, cookies was created with some third party libraries

Yes, there is method to delete:

setcookie($name, '', time() - 1000, "/");
setcookie($name, '', time() - 1000);

But is it "ideally" correct approach? do all browsers (including old) support this?

Any way, what is the method to correctly delete all cookies for sure for all browsers

How can I set exactly the same params deleting cookies (If I don't track cookies creation)?

Teimuraz
  • 8,795
  • 5
  • 35
  • 62
  • "If I don't track cookies creation" –  Sep 19 '16 at 01:04
  • @nogad, didn't get you – Teimuraz Sep 19 '16 at 01:07
  • @nogad He knows what parameters were used by cookies he created in his code, but he doesn't know what parameters were used if the cookie was created by a third-party library. – Barmar Sep 19 '16 at 01:19
  • sorry i was called away while wrtting that. –  Sep 19 '16 at 01:20
  • If a library creates cookies, and it doesn't use default parameters, I would expect it to provide a way for you to configure the parameters. For instance, PHP's `session_set_cookie_params` and `session_get_cookie_params` are for the `PHPSESSID` cookie. – Barmar Sep 19 '16 at 01:20

1 Answers1

2
if (isset($_SERVER['HTTP_COOKIE'])) {//do we have any
    $cookies = explode(';', $_SERVER['HTTP_COOKIE']);//get all cookies 
    foreach($cookies as $cookie) {//loop
        $parts = explode('=', $cookie);//get the bits we need
        $name = trim($parts[0]);
        setcookie($name, '', time()-1000);//kill it
        setcookie($name, '', time()-1000, '/');//kill it more
    }
}

from the notes: http://php.net/manual/en/function.setcookie.php