0

I want to delete a cookie and it does not work.

Here is the code, any ideas?

    setcookie("candidate_site_search", serialize($model->getAttributes()), strtotime('next year'), '/');

    if (isset($_GET['clearFilters']) && ($_GET['clearFilters'] == 1)) {
        //die('cf');
        $model->unsetAttributes();
        setcookie("candidate_site_search", serialize($model->getAttributes()), time() - 60 * 60 * 24 * 30);
        if (isset($_COOKIE['candidate_site_search']))
            unset($_COOKIE['candidate_site_search']);
        if (isset($_COOKIE['site_search']))
            unset($_COOKIE['site_search']);
        $this->redirect(array('/candidate/search'));
    }

After delete, i redirect to the same url from where the request came.

still nothing happens

p.s: I think this cookie is related to Chuck Norris, or at least Superman ...

Ionut Flavius Pogacian
  • 4,750
  • 14
  • 58
  • 100

1 Answers1

2

The problem is most likely in the date/time you have set on your server in relation to the time on the client.

For instance if your server is GMT+2 and your client is GMT, then the code above will tell the client to expire the cookie at GMT+1 which is still in the future for the client.

Just use a time that is waaay back in the past so you eliminate any time zone issues:

setcookie("name_of_cookie", "", time() - 60 * 60 * 24 * 30); // should  do it
Jan Hančič
  • 53,269
  • 16
  • 95
  • 99