我正在一个PHP项目中,我需要清除浏览器关闭点击浏览器。清除会话浏览器退出php
我的项目:
的index.php - > userdata.php - > reports.php - > finalreport.html
是有可能处理会话毁灭?
我需要清除会话,只要用户在任何页面中时退出浏览器。
请让我知道我们该如何处理这件事。
我正在一个PHP项目中,我需要清除浏览器关闭点击浏览器。清除会话浏览器退出php
我的项目:
的index.php - > userdata.php - > reports.php - > finalreport.html
是有可能处理会话毁灭?
我需要清除会话,只要用户在任何页面中时退出浏览器。
请让我知道我们该如何处理这件事。
会话在用户关闭浏览器**时被销毁。如果您希望在用户卸载页面后尽快将其销毁,则可以向页面卸载事件(类似jquery unload)添加处理程序,并对刚刚清除会话的脚本执行ajax请求。
编辑:根据OP的要求,我会添加特定的代码。中的所有页面
1)(的index.php,userdata.php,reports.php,finalreport.html)添加此在session_destroyer.php使用JavaScript代码
$(window).unload(function() {
$.get('session_destroyer.php');
});
2)这个代码(从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中默认情况下,我认为)
浏览器会话结束时,浏览器已经放弃[会话cookie](http://en.wikipedia.org/wiki/HTTP_cookie#Session_cookie);这还不够,还是这样? – Gumbo 2012-02-03 10:27:03
是关于浏览器会话还是“服务器会话”($ _SESSION的内容) – J0HN 2012-02-03 10:34:45