2013-01-16 148 views
2

我有這個功能設置cookie:PHP不能刪除的cookie

private function setCookie($key, $value){ 
     if(setcookie(
      $key, 
      $this->encrypt($value), 
      time() + 2592000, 
      adminpath, 
      database::getDomain(), 
      database::getHTTPS(), 
      true 
     )){ // set cookie for a month 
      return true; 
     } 
     else{ // cookie could not be created, write to errorlog 
      $error = array(
       "type"  => "other", 
       "argument" => 1, 
       "class"  => __CLASS__, 
       "function" => __FUNCTION__, 
       "errorMsg" => "Could not create cookie ".$key." with value ".$value, 
       "file"  => __FILE__, 
       "line"  => __LINE__ 
      ); 
      $this->errorlog->log($error); 
      return false; 
     } 
    } 

然後我用這個代碼來取消該cookie:

private function destroyCookie($key){ 
     if(setcookie(
      $key, 
      " ", 
      time() - (time() + 2592000), 
      adminpath, 
      database::getDomain(), 
      database::getHTTPS(), 
      true 
     )){ 
      return true; 
     } 
     else{ 
      $error = array(
       "type"  => "other", 
       "argument" => 1, 
       "class"  => __CLASS__, 
       "function" => __FUNCTION__, 
       "errorMsg" => "Could not destroy cookie ".$key, 
       "file"  => __FILE__, 
       "line"  => __LINE__ 
      ); 
      $this->errorlog->log($error); 
      return false; 
     } 
    } 

我可能失去了一些東西很簡單,但我不明白爲什麼我的cookie不會被刪除。 這兩個函數都在同一個類中,並且函數database :: getDomain()的結果爲「www.creetab.com」,函數database :: getHTTPS()的結果爲「false」。 adminpath是「/ admin /」。 有人可以幫我解決這個問題嗎? 設置cookie工作正常,它只是刪除不起作用的cookie。

回答

0
$key, 
" ", 
time() - (time() + 2592000), 

應該是:

$key, 
"", 
time() - 2592000, 

否則你只是給它一個空的空間,這仍然是一個值的值。你想給它沒有任何價值,把它變空。如上所述設置到期時間。

+0

謝謝!這工作 – SheperdOfFire