php 当用户关闭浏览器而不单击注销时销毁或取消设置会话
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2839988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Destroy or unset session when user close the browser without clicking on logout
提问by nectar
Possible Duplicate:
How do I expire a PHP session after 30 minutes?
I am destroying all session var in logout.php and calling it when user click on logout, what is user does not click on logout.php but directly close the browser. how can i delete session then???
我正在销毁 logout.php 中的所有会话变量,并在用户单击注销时调用它,什么是用户不单击 logout.php 而是直接关闭浏览器。那我怎样才能删除会话???
采纳答案by Gumbo
You can set an expiration time for the session data, test it with each session_startcall and destroy the session if it's expired:
您可以为会话数据设置过期时间,在每次session_start调用时对其进行测试,并在会话过期时销毁会话:
session_start();
if (!isset($_SESSION['EXPIRES']) || $_SESSION['EXPIRES'] < time()+3600) {
session_destroy();
$_SESSION = array();
}
$_SESSION['EXPIRES'] = time() + 3600;
回答by ThiefMaster
You cannot. However, session cookies are usually sent without an expire time which means they are deleted when the browser is closed, so the session is lost anyway.
你不能。但是,会话 cookie 通常在没有过期时间的情况下发送,这意味着它们会在浏览器关闭时被删除,因此无论如何会话都会丢失。
回答by Matt Weldon
PHP sessions should automatically expire when the browser window closes providing you do not modify the Session Cookies expiration time. If this is not happening then I would assume that you have modified this in some way and we would require further details to assist.
如果您不修改会话 Cookie 的过期时间,PHP 会话应在浏览器窗口关闭时自动过期。如果这没有发生,那么我假设您已经以某种方式修改了它,我们需要更多详细信息来提供帮助。
回答by Tgr
If you do not set an expire time for the session cookie, it will be deleted when the user closes the browser, which has the same effect for most practical purposes (unless you are worried about storage or security). If that is not good enough, you could set sessions to expire very quickly and periodically refresh them via AJAX.
如果您没有为会话 cookie 设置过期时间,它会在用户关闭浏览器时被删除,这对于大多数实际用途具有相同的效果(除非您担心存储或安全性)。如果这还不够好,您可以将会话设置为非常快地过期并通过 AJAX 定期刷新它们。
回答by Martin Milan
The session can be set to simply expire the server doesn't hear from the client after a certain period of time. That's the direction you want to be looking in...
会话可以设置为简单地过期,服务器在一段时间后没有收到来自客户端的消息。这就是你想要寻找的方向......
Martin
马丁

