[英]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.