我正在处理一个PHP项目,在该项目中,我需要清除点击关闭浏览器的选项。
我的项目:
Index.php->userdata.php->reports.php->finalreport.html
是否可以处理会话销毁?
每当用户在任何页面中退出浏览器时,我都需要清除会话。
请告诉我我们该怎么处理。
当用户关闭浏览器**时,会话被破坏。如果您想在用户卸载页面后立即销毁它,您可以向页面卸载事件添加一个处理程序(类似于jqueryunload),并对只清除会话的脚本执行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中的默认会话)