最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Automatic log-out if no activity found on page - Stack Overflow

programmeradmin1浏览0评论

I want to make a script, which can automatically check whether the user is performing some activity on screen page. If no activity is received in 1 minute, then I want to alert him with asking reason of being idle. I think this can be done in jquery/javascript and php. Can someone please give me anything so that I can make my dream possible.

And if he don't clicks on any of the item, then automatic log-out after total 1 minutes.

I want to make a script, which can automatically check whether the user is performing some activity on screen page. If no activity is received in 1 minute, then I want to alert him with asking reason of being idle. I think this can be done in jquery/javascript and php. Can someone please give me anything so that I can make my dream possible.

And if he don't clicks on any of the item, then automatic log-out after total 1 minutes.

Share Improve this question asked May 4, 2012 at 6:46 ThompsonThompson 2,00010 gold badges37 silver badges59 bronze badges 3
  • what is it that you have tried ? – Satya Commented May 4, 2012 at 6:48
  • i donot have any idea how to implement this. Thats why i asked here. – Thompson Commented May 4, 2012 at 6:48
  • 1 Try setTimeout() in js, but put some effort into figuring it out on your own first. If you don't put any effort into it, why should we? – Blake Commented May 4, 2012 at 6:49
Add a ment  | 

3 Answers 3

Reset to default 3
  • set a variable to indicate user activity, initially false
  • set a variable indicating the current time
  • set a document wide click handler to set user activity to true when a user clicks anywhere in the page
  • set a listener function, using setTimeout, to check the time past and the first variable. If it's true, cancel listening, otherwise if the time past is < a minute continue listening, if time past > 1 minute alert the user and take your action

See this jsfiddle for a q&d example of this algorithm

Good luck!

idle plugin

This plugin do the trick

<!-- 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

}
});

and using ajax you can send the request to server to close the session

You have to add different event handlers on body tag to determine if user is inactive. I think onmousemove, oncllick and onkeypress events will be enough.

  • you should start a timer and when it hits a specific time you just go to the log out URL
  • implement mentioned event handlers. Inside them reset the timer.

This will do.

发布评论

评论列表(0)

  1. 暂无评论