如果用户在一段时间内处于非活动状态,则强制注销用户 [英] Force Logout users if users are inactive for a certain period of time

查看:810
本文介绍了如果用户在一段时间内处于非活动状态,则强制注销用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您正在执行银行业务应用程序.如果用户登录到您的站点,如何检测他们的不活动状态并要求他们注销他们是否在一段时间内保持不活动状态?在此处于非活动状态意味着他们要么切换到其他选项卡,要么不触摸浏览器应用程序.

Assume that you are doing a banking application. If users are logged into your site, how to detect their inactivity and ask them to log out if they remain inactive for a period of time? Inactive here means they have either switch to other tabs, or not touching the browser application.

我想我可以通过在用户在应用程序的每个页面上进行操作时注册每个鼠标移动或键盘移动来做到这一点.但是该代码非常丑陋且难以维护.还有其他更优雅的方法吗?

I guess think I can do this by registering every mouse movement or keyboard movement when users are doing on EVERY page of my application. But the code would be very ugly and hard to maintain. Is there other more elegant ways of doing this?

推荐答案

如果用户定期从服务器请求新的页面/数据,则在PHP中调整会话超时应该可以解决此问题(假设您正在使用PHP会话).

If the user is requesting new pages/data from your server on a regular basis, then adjusting the session timeout in PHP should work for this (assuming you are using PHP sessions).

如果担心的是他们可能长时间停留在一页上而没有前往服务器的行程(例如,填写较长的表格),而您想区别于此,则用户只需切换到另一页即可在窗口中,您可以执行类似的操作,每隔五分钟左右使用javascript使用XMLHTTPRequest请求一些数据,以保持会话有效.您可以在javascript中使用window.focus和window.onblur事件来停止和重新启动此机制(我认为IE有一些区别,很好的解释

If the concern is that they could be sitting on one page for a good length of time with no trips to the server (e.g. filling out a long form), and you want to distinguish between this and the user simply switching to another window, you could do something like use javascript to request some data using XMLHTTPRequest every five minutes or so to keep the session alive. You could use the window.focus and window.onblur events in javascript to stop and restart this mechanism (I think there are some differences for IE, there is a good explanation here).

这篇关于如果用户在一段时间内处于非活动状态,则强制注销用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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