2012-02-06 99 views
3

奇怪的問題。Cookie未刪除

這是index.php文件:

session_start(); 
print_r($_COOKIE); 
print_r($_SESSION); 

這是logout.php:

session_destroy(); 
$_COOKIE['key'] = ""; 
$_COOKIE['usr_email'] = ""; 
setcookie("key", "", time() - 3600); 
setcookie("usr_email", "", time() - 3600); 
header("Location: http://www.site.net/index.php"); 

當我打開logout.php,被重定向到index.php後我得到:

Array 
(
    [fc] => fcVal=6927578914025605120 
    [PHPSESSID] => na015ipu3s69hhj00sgd0h1es6 
) 
Array 
(
    [key] => cc2bffe0c1e36bc5790f5b78b11e5f50 
    [usr_email] => [email protected] 
) 

因爲我在加載index.php時生成它們,所以會話沒問題,但cookie仍然存在?

注意

我有一些的index.php更多的代碼,決定是否登錄表單中瀰漫,並開始對用戶進行認證,如果用戶名/密碼是正確的它設置兩屆瓦爾和兩個餅乾vars(usr_email和key)。

目前,我在登錄表單之後,因此我有Cookie和會話變量,並且由於Cookie未被刪除,因此我禁用了索引頁上的其他任何PHP代碼會自動登錄。因此,目前我已經通過身份驗證,然後從索引頁面註釋掉了上面沒有提到的其他內容。

也與此有關 我這是怎麼設置的Cookie當一個用戶進行身份驗證:

setcookie("key", $cookie['key'], time() + 36000); 
    setcookie("usr_email", $cookie['usr_email'], time() + 36000); 

PHP版本:PHP 5.3.6-13

最後編輯

正如Josh在評論中寫道的,我將cookie打印與會話混淆,我無法刪除會話變量而不是cookie變量。

+0

你試過'退出'腳本後頭:位置? – TimWolla 2012-02-06 19:31:38

+0

@TimWolla - 剛試過,不會改變任何東西。 – 2012-02-06 19:33:10

+2

請勿將相對時間用於您的Cookie刪除時間戳。這假定用戶的時鐘是(相對)準確的。設置一個固定的「1月1日19:00 00:00:00格林尼治標準時間」類型的時間戳,以便即使是完全損壞時鐘的機器應該最有可能兌現它。 – 2012-02-06 19:33:15

回答

2

PHP Manual

... [session_destroy()]不取消任何與 會話,或取消會話cookie相關的全局變量...

爲了完全終止會話,喜歡註銷用戶,則 會話ID也必須給予取消。如果使用cookie傳播會話ID(默認行爲),那麼會話cookie必須被刪除,其中 。 setcookie()可用於此目的。

注:PHPSESSID是餅乾傳播的會話ID。

請嘗試以下方法被重用清除您的會話變量:

// Example #1 Destroying a session with $_SESSION 
// Initialize the session. 
// If you are using session_name("something"), don't forget it now! 
session_start(); 

// Unset all of the session variables. 
$_SESSION = array(); 

// If it's desired to kill the session, also delete the session cookie. 
// Note: This will destroy the session, and not just the session data! 
if (ini_get("session.use_cookies")) { 
    $params = session_get_cookie_params(); 
    setcookie(session_name(), '', time() - 42000, 
     $params["path"], $params["domain"], 
     $params["secure"], $params["httponly"] 
    ); 
} 

// Finally, destroy the session. 
session_destroy(); 

注:session_start()之前,必須調用session_destroy()可以工作。

+0

這很有效,謝謝。 – 2012-02-06 19:54:49

0

您是否在子目錄中創建cookie?如果是這樣,請確保您已添加Cookie路徑。下面是一個例子

setcookie("usr_email", "", time() - 3600, '/directory-name/'); 
+0

看到我的編輯,我不在自定義路徑中設置cookie – 2012-02-06 19:35:03