3

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)?

5
  • "If I don't track cookies creation" Commented Sep 19, 2016 at 1:04
  • @nogad, didn't get you Commented Sep 19, 2016 at 1: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. Commented Sep 19, 2016 at 1:19
  • sorry i was called away while wrtting that. Commented Sep 19, 2016 at 1: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. Commented Sep 19, 2016 at 1:20

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.