2013-03-18 94 views
0

我想刪除一個cookie,它不起作用。我的cookie不想刪除自己

這裏是代碼,任何想法?

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

刪除後,我重定向到請求來的地址。

仍然沒有任何反應

PS:我覺得這個cookie是與查克·諾里斯,或至少超人......

+0

這是否'if'評價真的嗎? – 2013-03-18 08:03:57

+0

使用與設置cookie時相同的可選參數來刪除cookie。像路徑,域名等。 – 2013-03-18 08:04:03

+0

檢查此[http://stackoverflow.com/questions/686155/remove-a-cookie](http://stackoverflow.com/questions/686155/remove-a-cookie) – 2013-03-18 08:04:17

回答

2

的問題是最有可能在你的服務器上設置的日期/時間關於客戶的時間。

舉例來說,如果你的服務器是GMT + 2和你的客戶是GMT,那麼上面的代碼會告訴客戶在到期GMT + 1餅乾這仍然是在未來的客戶端。

只需使用一次即waaay回到了過去,所以你消除任何時區的問題:

setcookie("name_of_cookie", "", time() - 60 * 60 * 24 * 30); // should do it 
+0

嗯,這可能是 – 2013-03-18 08:06:28

+0

它不是這個:( – 2013-03-18 08:07:37

+0

+1爲原始想法 – 2013-03-18 08:08:03