浏览器退出php的清除会话

vin*_*eel 0 php session

我正在开发一个PHP项目,我需要在点击浏览器关闭时清除.

我的项目 :

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

是否有可能处理会话破坏?

每当用户在任何页面中退出浏览器时,我都需要清除会话.

请让我知道我们如何处理这个问题.

Bog*_*dan 5

当用户关闭浏览器**时会破坏会话.如果你想在用户卸载页面时立即销毁它,你可以在页面卸载事件(类似于jquery unload)中添加一个处理程序,并对只清除会话的脚本执行ajax请求.

编辑:根据OP的要求,我将添加特定代码.

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

 $(window).unload(function() {
   $.get('session_destroyer.php');
 }); 
Run Code Online (Sandbox Code Playgroud)

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();
?>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助

**注意:正如一位评论者指出的那样,这假设您正在使用基于cookie的会话(我认为这是PHP的默认设置)