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

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

问题描述

假设您正在做一个银行应用程序.如果用户登录了您的站点,如何检测他们的不活动状态并要求他们在一段时间内保持不活动状态时退出?这里的非活动意味着他们要么切换到其他选项卡,要么不接触浏览器应用程序.

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天全站免登陆