如何使用jQuery和PHP检查空闲时间? [英] How to check idle time using jQuery and PHP?

查看:101
本文介绍了如何使用jQuery和PHP检查空闲时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用PHP SESSION的脚本.我使用类管理PHP会话.在此类中,我有一个方法可以返回会话过期前剩余的秒数.

I have a script that uses PHP SESSION. I manage PHP sessions using a class. Within this class I have a method that returns how many seconds remaining before the session expires.

另外,传达用户刷新页面或打开新页面(对服务器的新请求)的时间,空闲时间计数器从$_SESSION['MA_IDLE_TIMEOUT'] = time()+900;重新开始.

Also, Evey time the user refresh the page or open a new page (new request to the server) the idle time counter starts over $_SESSION['MA_IDLE_TIMEOUT'] = time()+900;

我想做的是在PHP会话到期前2分钟显示一条对话框消息,并检查用户是否仍在页面上.如果用户单击保持工作",则jQuery脚本将发送PHP AJAX请求以续订会话$_SESSION['MA_IDLE_TIMEOUT'] = time()+900;.如果用户未单击任何内容或单击注销",则会话结束,并且该用户将重定向到登录页面.

What I am looking to do is display a dialog message 2 minutes before the PHP session expire and check if the user is still on the page or not. If the use clicks "Keep working" then the jQuery script will send PHP AJAX request to renew the session $_SESSION['MA_IDLE_TIMEOUT'] = time()+900;. If the user did not click anything or clicked on "Log out" then the session ends and the user is redirected to the login page.

我确实找到了一个不错的插件,可以一定程度地完成Job jquery-idle-timeout

I did find a nice plugin that will somewhat does the Job jquery-idle-timeout

此插件的问题是它检查用户是否在使用JavaScript(如果正在使用键盘/鼠标)闲置.这是该脚本无法帮助我的问题:可以说我的PHP会话限制为15分钟/900秒.用户正在阅读同一页面上的超长文章.他/她将滚动,从JavaScript的角度来看,它们并不是真正的空闲,但是从PHP的角度来看,使用是空闲的.然后,在20分钟后,用户刷新页面,然后由于该用户在900秒的限制内未向PHP服务器发送新请求,因此该用户将注销.

The issue with this plugin is that it checks if the user is idle using JavaScript (if the keyboard/mouse) are being used. Here is the senario where this script does not help me: lets say my PHP sessions has a limit on 15 minutes/ 900 seconds. A user is reading a super long article on the same page. He/she will be scrolling, they are not really idle "from JavaScript perspective" but from a PHP perspective the use is idle. Then after 20 minutes the user refresh the page, then the user will be logged out since he/she have not sent a new request to the PHP server for over the 900 seconds limit.

我该如何解决这个问题?有没有更好的插件可以做到这一点?如果我在此插件中错过了可以解决我问题的东西?

How can I solve this problem? is there a better plugin to does the trick? if there something I missed in this plugin that will solve my problem?

谢谢

推荐答案

如果用户未发出请求,并且没有移动鼠标或键盘或触摸设备等,则从应用程序的角度来看,用户即使他们的眼球不在,也是空闲".

If the user is not making requests, and is not moving the mouse or keyboard or touching the device, etc., then from the app's point of view the user is "idle" even if their eyeballs are not.

如果用户正在滚动,则可以使用javascript来监听滚动事件(例如,通过onscroll),但这并不是100%可靠的,因为(1)依赖于javascript,而(2)不依赖于javascript如果您正在查看短文章或使用高/大幅面监视器(例如,可以旋转90度的监视器),则可以正常工作.

If the user is scrolling you can use javascript to listen for scroll events (via onscroll, for example), but this will not be 100% reliable because (1) depends on javascript, and (2) doesn't work if you are viewing a short article or using a tall/large format monitor (like the ones you can rotate 90 degrees, for example).

也许您可以用不同的方式处理:使用cookie,单点登录或类似技术来预先认证或自动认证请求,以便用户会话可以安全地终止并重新启动,而无需用户手动登录.

Perhaps you could handle this differently: use cookies, single sign-on or similar techniques to pre-authenticate or automatically authenticate requests so that the user's session can safely die and be restarted without the user having to manually login.

您可以处理此问题的另一种方法是维护一个"ping"过程,该过程会例行ping服务器(例如,通过setInterval())以保持会话的活动状态,并使用单独的超时(例如身份验证"之类的东西) (ASP.NET使用的超时")来跟踪应何时注销空闲"用户.然后,用户操作(例如滚动,请求页面,在字段中聚焦,移动鼠标等)可以执行"ping重置",将空闲计数器重置为0.

The other way you could handle this is to maintain a "ping" process that routinely pings the server (via setInterval(), for example) to keep the session alive, and uses a separate timeout (maybe something like the "Authentication timeout" that ASP.NET uses) to keep track of when the "idle" user should be logged out. Then user actions such as scrolling, requesting pages, focusing in fields, moving mouse, etc., can do a "ping reset" that resets the idle counter to 0.

示例/概念-留给读者练习以完善它:

Example / Concept - leaving as exercise for reader to perfect it:

var idleTime = 0; // how long user is idle
var idleTimeout = 1000 * 60 * 20; // logout if user is idle for 20 mins
var pingFrequency = 1000 * 60; // ping every 60 seconds
var warningTime = 1000 * 60 * 2; // warning at 2 mins left
var warningVisible = false; // whether user has been warned
setInterval(SendPing, pingFrequency);
setInterval(IdleCounter, 1000); // fire every second
function IdleCounter() {
    idleTime += 1000; // update idleTime (possible logic flaws here; untested example)
    if (console) console.log("Idle time incremented. Now = " + idleTime.toString());
}

function SendPing() {
    if (idleTime < idleTimeout) {
        // keep pinging
        var pingUrl = "tools/keepSessionAlive.php?idleTime=" + idleTime;
        $.ajax({
            url: pingUrl,
            success: function () {
                if (console) console.log("Ping response received");
            },
            error: function () {
                if (console) console.log("Ping response error");
            }
        });

        // if 2 mins left, could show a warning with "Keep me on" button
        if ((idleTime <= (idleTimeout - (idleTimeout - warningTime))) && !warningVisible) {
            ShowTimeoutWarning();
        }
    } else {
        // user idle too long, kick 'em out!
        if (console) console.log("Idle timeout reached, logging user out..");
        alert("You will be logged off now dude");
        window.location.href = "logoff.aspx"; // redirect to "bye" page that kills session
    }
}

function ShowTimeoutWarning() {
    // use jQuery UI dialog or something fun for the warning
    // when user clicks OK set warningVisible = false, and idleTime = 0
    if (console) console.log("User was warned of impending logoff");
}

function ResetIdleTime() {
    // user did something; reset idle counter
    idleTime = 0;
    if (console) console.log("Idle time reset to 0");
}
$(document) // various events that can reset idle time
.on("mousemove", ResetIdleTime)
    .on("click", ResetIdleTime)
    .on("keydown", ResetIdleTime)
    .children("body")
    .on("scroll", ResetIdleTime);

这篇关于如何使用jQuery和PHP检查空闲时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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