浏览器关闭时是否有可靠的方法来注销用户?
我正在寻找一种可靠的方法来在浏览器关闭时注销用户或放弃他们的会话。有没有办法用选项卡式浏览器来做到这一点?任何建议表示赞赏。谢谢!
I am looking for a reliable way to log out a user or abandon their session when the browser is closed. Is there a way to do this with tabbed browsers?? Any suggestions are appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当客户端关闭浏览器时,没有可靠的方法可以立即执行此操作。有
beforeunload
事件,但即使如此,当您在此事件期间触发 ajax 请求时,也不能保证它能够到达服务器。尽管如此,您仍然会遇到多个浏览器选项卡的问题。最可靠的方法是在服务器端设置相对较短的会话超时(例如1分钟),并在客户端引入基于ajax的心跳(例如每30秒)以保持会话活动。
可能有更好的方法,具体取决于您认为这是解决方案的唯一功能要求。例如,如果您的实际意图是将每个注册用户的所有登录限制为 1 次,那么您最好收集所有登录和关联的会话,然后在每次登录时进行比较,并在以下情况下使之前的会话无效:任何礼物。这样它在禁用 JS 的客户端上也能正常工作。
There is no reliable way to do this immediately when the client closes the browser. There's the
beforeunload
event, but even then, when you fire an ajax request during this event, it's not guaranteed to ever reach the server. Still then, you've a problem with multiple browser tabs.Most reliable way is to have a relatively short session timeout in the server side (e.g. 1 minute) and introduce an ajaxbased heartbeat on the client side (e.g. every 30 seconds) to keep the session alive.
There may be better ways depending on the sole functional requirement for which you thought that this is the solution. For example, if your actual intent is to restrict all logins to 1 per registered user, then you'd better collect all logins and the associated sessions and then compare this on each login and invalidate the earlier session if any present. This way it'll work as well on clients with JS disabled.
如果您不使用 cookie 来保存用户的登录信息,那么当用户关闭浏览器时,它应该将用户注销,因为当浏览器关闭时,任何会话 cookie 都应该被终止。
显然情况并非总是如此(请参阅此处的示例,Firefox 在注销后保留登录信息)因为“会话恢复”功能,我们现在模糊了“单个浏览器会话”之间的界限。 (我个人认为这应该被归类为错误,但这只是我的意见)。
有两种可能的技术。第一种是(正如 yojimbo87 在我之前提到的)使用 Web 套接字来保持客户端和服务器之间的连接,并且当套接字关闭时,终止会话。这里的问题是 Web 套接字支持是有限的,并且除了尖端浏览器(FF4、Chrome、IE9 等)之外,当然不可能在任何其他浏览器上支持。
另一种方法是使用 AJAX 不断轮询服务器,告诉它页面仍在被查看,因此,例如,如果您每 30 秒通过 AJAX 发送一个保持活动请求,您将存储该页面的时间戳会话中的请求。如果用户随后返回该页面,并且当前请求与上一个请求之间的时间差超过...45 秒(考虑到延迟),您就会知道用户关闭了浏览器并且需要登录再次进入。
然而这两种情况都有一个致命的缺陷,那就是它们依赖于 JavaScript。如果用户没有启用 JavaScript,那么不断的登录提示最终会破坏用户体验,这显然是一个坏主意。
在我看来,我认为简单地依赖当用户关闭浏览器窗口时浏览器删除会话 cookie 是合理的,因为这是他们应该做的。当客户端浏览器执行不良行为时,您作为开发人员不能受到指责,因为这完全超出您的控制范围,并且没有功能性的解决方法。
If you aren't using cookies to preserve your users' login information, it should log them out when they close the browser, because any session cookies should be killed when the browser closes.
Obviously this isn't always the case (see here for an example of Firefox preserving login information after logging out) because "session restore" features we now blur the line between what is considered a "single browser session". (Personally, I think this should be classified as a bug, but that is only my opinion).
There are two possible techniques. The first would be (as yojimbo87 mentions before me) to use web sockets to keep a connection between client and server, and when the socket closes, kill the session. The issue here is that web sockets support is limited, and certainly not possible on anything other than bleeding edge browsers (FF4, Chrome, IE9, etc).
An alternative could be to use AJAX to constantly poll the server to tell it that the page is still being viewed, so if, for example, you send a keep-alive request via AJAX every 30 seconds, you'd store the timestamp of the request in the session. If the user then comes back to the page and the time difference between the current request and the last request is more than say... 45 seconds (accounting for latency), you'd know that the user closed their browser and need to log in again.
In both of these situations, there is however a fatal flaw, and that is that they rely on JavaScript. If the user doesn't have JavaScript enabled, you'd end up ruining the user experience with constant login prompts, which is obviously a bad idea.
In my opinion, I think its reasonable to simply rely on session cookies being deleted by the browser when the user closes the browser window, because that is what they are supposed to do. You as a developer can't be blamed when the client browser performs undesirable behaviour, since its entirely out of your hands, and there's no functional workaround.
一种可行的技术是使用 AJAX 经常向您的服务器发送保持活动请求 - 例如每隔一分钟。然后,一旦未按预期收到保持活动状态(或连续的几个保持活动状态),您就可以放弃会话。
否则,没有可靠的方法来实现这一目标。由于浏览器和服务器之间没有持久连接,因此您无法检测到浏览器中运行的任何 JavaScript 代码失控的情况。例如,当出现网络故障时,即使浏览器的窗口仍然打开,您可能也想关闭会话。因此,为了使系统足够健壮,您应该将网络中断检测为浏览器保持活动机制的“副作用”(例如,像 Gmail 那样)。
A feasible technique would be to use AJAX to send keep-alive requests to your servers quite often — e.g. every one minute. Then you could abandon a session as soon as a keep-alive (or a few in sequence) is not received as expected.
Otherwise, there's no reliable way to achieve that. Since there's not a persistent connection between the browser and the server you can't detect situations that are out-of-control of any JavaScript code you might have running in the browser. For example, when there's a network failure you might want to close the session as well even though the browser's window is still opened. Hence, to make the system robust enough, you should detect network outages as a “side-effect” of the keep-alive mechanism from the browser (e.g. like Gmail does it).
除非您使用 WebSockets 或对每个选项卡进行某种长轮询来“实时”跟踪与客户端的连接,否则您可能必须等到服务器端的会话超时。
Unless you are using WebSockets or some kind of long polling for each tab which tracks the connection with client in "real time", you will probably have to wait until the session is timed out on the server side.
您可以通过 Jquery、Ajax 和 PHP 的组合来完成此操作
Jquery
之前的卸载脚本
和 php 您必须自己编写,我非常确定您可以做到。
它不是最可靠的,但它是实现它的最简单的方法。如果你想要实时报告..看看彗星
You can do this via a combination of Jquery,Ajax and PHP
The Jquery
The before unload script
and the php you would have to write yourself which i'm more then certain you can do.
it isn't the most reliable but it's the easiest way to implement that. if you want real time reporting .. look into comet