簡體   English   中英

清除PHP的瀏覽器退出會話

[英]Clear session on browser exit for php

我正在開發一個PHP項目,在其中需要清除單擊瀏覽器關閉時的提示。

我的項目 :

Index.php-> userdata.php-> report.php-> finalreport.html

是否有可能處理會話破壞?

每當用戶在任何頁面中退出瀏覽器時,我都需要清除session。

請讓我知道我們該如何處理。

用戶關閉瀏覽器**時,會話將被破壞。 如果您想在用戶卸載頁面后立即銷毀它,則可以向頁面卸載事件中添加一個處理程序(類似於jquery unload ),並向僅清除會話的腳本發出ajax請求。

編輯:根據OP的要求,我將添加特定的代碼。

1)在所有頁面(Index.php,userdata.php,reports.php,finalreport.html)中添加此javascript代碼

 $(window).unload(function() {
   $.get('session_destroyer.php');
 }); 

2)在session_destroyer.php中使用此代碼(摘自php.net

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

希望這可以幫助

**注意:正如一位評論者所述,這假設您正在使用基於cookie的會話(我認為這是PHP中的默認會話)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM