2011-11-02 16 views
0

我創建了一個包含註銷按鈕的菜單的用戶頁面。當點擊該按鈕,用戶將被引導到一個註銷頁面下面的代碼:將自己的甜蜜時間銷燬的會話

session_start(); 
session_destroy(); 

include("/var/www/include/header.inc"); 
echo "<h>Logout Success</h>"; 
include("/var/www/include/menu.inc"); 
include("/var/www/include/footer.inc"); 

menu.inc文件中的代碼編寫這樣的:

if(@$_SESSION['login'] == "yes") 
{ 
// show expanded menu 
} 
else 
{ 
// show normal menu 
} 

我是什麼現在看到註銷後是擴展菜單。看起來菜單包含的速度比會話可能被破壞的速度快,因此產生用戶仍然登錄的印象。有沒有辦法避免這種情況?

+0

請務必檢查出[session_destroy(http://php.net/manual/en/function.session-destroy.php)(記住例子(和小字)有可能需要清理的cookies和其他信息)。 –

回答

1

session_destroy不會取消設置$ _SESSION數組,所以session_destroy後面的其餘部分仍然會看到它。你可以簡單地嘗試這種

session_destroy(); 
unset($_SESSION); 
+0

謝謝! 'unset'完成這項工作。 –

0

要徹底清除你必須使用類似的東西來

<?php 
// 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(); 
?> 

這是PHP手冊中介紹的所有會話數據:

session_destroy()銷燬所有與當前 相關的數據會話。它不會取消設置任何與會話相關的全局變量,也不會取消設置會話Cookie。要再次使用會話變量 ,必須調用session_start()。

爲了完全消除會話,就像登錄用戶一樣, 會話ID也必須取消設置。如果使用cookie傳播會話ID(默認行爲),那麼會話cookie必須被刪除,其中 。 setcookie()可用於此目的。

1

session_destroy()銷燬所有與當前會話相關的數據。 它不會取消設置與會話相關聯的任何全局變量,也不會取消設置會話Cookie。

Source

+0

剛剛提到。 –