如果在页面上未找到活动,则自动注销

Automatic log-out if no activity found on page

本文关键字:活动 注销 如果      更新时间:2023-09-26

我想制作一个脚本,它可以自动检查用户是否正在屏幕上执行某些活动。如果在 1 分钟内没有收到任何活动,那么我想提醒他询问空闲的原因。我认为这可以在jquery/javascript和php中完成。有人可以给我任何东西,这样我就可以实现我的梦想。

如果他没有点击任何项目,那么在总共 1 分钟后自动注销。

  • 设置一个变量以指示用户活动,最初为 false
  • 设置指示当前时间的变量
  • 设置文档范围单击处理程序,以便在用户单击页面中的任意位置时将用户活动设置为 True
  • 使用 setTimeout 设置侦听器函数以检查过去的时间和第一个变量。如果是真的,请取消收听,否则,如果过去的时间<一分钟,请继续收听,如果超过>则提醒用户并采取您的操作

有关此算法的问答示例,请参阅此jsfiddle

祝你好运!

空闲插件

这个插件可以解决问题

<!-- dialog window markup -->
<div id="dialog" title="Your session is about to expire!">
   <p>You will be logged off in <span id="dialog-countdown"></span> seconds.</p>
   <p>Do you want to continue your session?</p>
</div>

// setup the dialog
$("#dialog").dialog({
 autoOpen: false,
 modal: true,
 width: 400,
 height: 200,
 closeOnEscape: false,
 draggable: false,
 resizable: false,
 buttons: {
    'Yes, Keep Working': function(){
        // Just close the dialog. We pass a reference to this
        // button during the init of the script, so it'll automatically
        // resume once clicked
        $(this).dialog('close');
    },
    'No, Logoff': function(){
        // fire whatever the configured onTimeout callback is.
        $.idleTimeout.options.onTimeout.call(this);
    }
 }
});
// start the plugin
$.idleTimeout('#dialog', 'div.ui-dialog-buttonpane button:first', {
idleAfter: 300, // user is considered idle after 5 minutes of no movement
pollingInterval: 60, // a request to keepalive.php (below) will be sent to the server every minute
keepAliveURL: 'keepalive.php',
serverResponseEquals: 'OK', // the response from keepalive.php must equal the text "OK"
onTimeout: function(){
    // redirect the user when they timeout.
    window.location = "timeout.htm";
},
onIdle: function(){
    // show the dialog when the user idles
    $(this).dialog("open");
},
onCountdown: function(counter){
    // update the counter span inside the dialog during each second of the countdown
    $("#dialog-countdown").html(counter);
},
onResume: function(){
    // the dialog is closed by a button in the dialog
    // no need to do anything else
}
});

并使用 Ajax,您可以将请求发送到服务器以关闭会话

您必须在 body 标记上添加不同的事件处理程序,以确定用户是否处于非活动状态。我认为onmousemove,oncllick和onkeypress事件就足够了。

  • 您应该启动一个计时器,当它达到特定时间时,您只需转到注销 URL
  • 实现提到的事件处理程序。在它们内部重置计时器。

这样就行了。