2012-02-03 151 views
0

我正在一个PHP项目中,我需要清除浏览器关闭点击浏览器。清除会话浏览器退出php

我的项目:

的index.php - > userdata.php - > reports.php - > finalreport.html

是有可能处理会话毁灭?

我需要清除会话,只要用户在任何页面中时退出浏览器。

请让我知道我们该如何处理这件事。

+1

浏览器会话结束时,浏览器已经放弃[会话cookie](http://en.wikipedia.org/wiki/HTTP_cookie#Session_cookie);这还不够,还是这样? – Gumbo 2012-02-03 10:27:03

+0

是关于浏览器会话还是“服务器会话”($ _SESSION的内容) – J0HN 2012-02-03 10:34:45

回答

5

会话在用户关闭浏览器**时被销毁。如果您希望在用户卸载页面后尽快将其销毁,则可以向页面卸载事件(类似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中默认情况下,我认为)

+0

你错了。浏览器只会丢弃会话的cookie(如果它实际上是[会话cookie](http://en.wikipedia.org/wiki/HTTP_cookie#Session_cookie))。实际会话仍然存在。 – Gumbo 2012-02-03 10:28:57

+0

但我想删除会话被破坏时创建的临时文件 – vineel 2012-02-03 10:29:24

+0

查看此页面上的第一个示例http://www.php.net/manual/en/function.session-destroy.php – Bogdan 2012-02-03 10:33:01