如何在浏览器关闭时结束会话 [英] How to end the session when browser closes

查看:357
本文介绍了如何在浏览器关闭时结束会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我将保存用户的注销时间。这是工作正常,当用户单击登录按钮,但我的要求是存储注销时间,即使用户关闭浏览器。如果这不可能,请告诉我如何在用户关闭窗口后立即销毁会话。

In my application I am saving the logout time of the user. This is working fine when the user clicks the login button, but my requirement is to store the logout time even if the user closes the browser. If this is not possible please tell me the way how to destroy the session as soon as user closes the window.

任何帮助将不胜感激。

推荐答案

过去,你必须在事件处理程序中执行同步 Ajax。不知道这在现代浏览器是否仍然如此。如果您进行异步,请务必使用现实的客户端负载进行测试,以确保您不会遇到只在用户之间显示的竞争条件。

It used to be you had to do synchronous Ajax in the event handler. Don't know if that's still true in modern browsers. If you go asynchronous, be sure to test with realistic client load so that you don't run into a race condition that only shows up among your users.

我建议某种心跳从客户端打Ajax调用,说每五分钟。如果你错过两个这样的调用在一行中,你杀死会话服务器端。这将照顾所有用户未注销的情况(浏览器崩溃,操作系统崩溃,网络故障等)。

I would suggest some kind of heart beat Ajax call from the client, say every five minutes. If you miss two such calls in a row you kill the session server-side. That will take care of all situations where the user doesn't log out (browser crash, OS crash, network failure etc).

对卸载事件执行操作时可能遇到的问题:

Potential problems with acting on unload events:


  • 如果用户的应用/网站打开了几个标签页并关闭其中的一个,会发生什么?您需要通知用户将出现交叉表注销。

  • 浏览器对于您可以在卸载时执行的操作有历史记录,以防止永远弹出和无限循环,从而防止用户关闭浏览器。你永远不知道未来会有什么。历史记录: http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript ,并通过StackOverflow链接在上面的回复中。

  • What happens if the user has your app/site open i several tabs and closes one of them? You need to inform the user that there will be a cross-tab logout.
  • Browsers have a history of restrictions on what you can do on unload to prevent ever-popups and infinite loops that prevent the user from closing the browser. You never know what will come in the future. History here: http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript and via the StackOverflow link in the reply above.

这篇关于如何在浏览器关闭时结束会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆