BBKing
BBKing

Reputation: 2299

auto logout idle timeout using jquery php

I searched and find some examples to set idle timeout using jquery.

1 - Idle Timeout By Eric Hynds DEMO

2 - Idle Timer By paulirish

3 - Fire Event When User is Idle / DEMO HERE

4 - detect user is active or idle on web page

5 - Comet Long Polling with PHP and jQuery

6 - detacting idle timeout javascript

... And several other similar examples

Between these examples number 1 is better for i need because i need to auto logout user with any confirm alert after X minutes (logout.php or any url). but this method Not suitable for server. problem is : this jquery code send ping to any url : keepAlive.php in loop/pooling for request OK text . see firebug screen :

enter image description here

how to fix this ? So, other examples only printed Idle/No Idle and not work with alert confirm and auto logout ( logout.php or any url ) now really better way to choose idle timeout using jquery/Php ?

Thanks

Upvotes: 1

Views: 16017

Answers (4)

Shamim Shaikh
Shamim Shaikh

Reputation: 827

Easy and simle

     var autoLogoutTimer;
        resetTimer();
        $(document).on('mouseover mousedown touchstart click keydown mousewheel DDMouseScroll wheel scroll',document,function(e){
            // console.log(e.type); // Uncomment this line to check which event is occured
            resetTimer();
        });
        // resetTimer is used to reset logout (redirect to logout) time 
        function resetTimer(){ 
            clearTimeout(autoLogoutTimer)
            autoLogoutTimer = setTimeout(idleLogout,5000) // 1000 = 1 second
        } 
        // idleLogout is used to Actual navigate to logout
        function idleLogout(){
            window.location.href = ''; // Here goes to your logout url 
        }

Upvotes: 0

Parmar Satish
Parmar Satish

Reputation: 1

<script>    
var idleMax = 5;  (5 min)
var idleTime = 0;
(function ($) {

    $(document).ready(function () {

        $('*').bind('mousemove keydown scroll', function () {
            idleTime = 0; 
            var idleInterval = setInterval("timerIncrement()", 60000); 
       });
        $("body").trigger("mousemove");

    });
}) (jQuery)
function timerIncrement() {
     idleTime = idleTime + 1;
     if (idleTime > idleMax) { 
         window.location="Your LOGOUT or Riderct page url here";
     }
 }
</script>

Upvotes: 0

JAR
JAR

Reputation: 11

Here is my approach that I applied to build a simple auto-logout feature with JavaScript and jQuery. This script was constructed for use with webpages that will automatically go to the logout page when mouse movement is not detected within 25 minutes.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script type="text/javascript" language="javascript">
  var idleMax = 25; // Logout after 25 minutes of IDLE
  var idleTime = 0;

  var idleInterval = setInterval("timerIncrement()", 60000);  // 1 minute interval    
  $( "body" ).mousemove(function( event ) {
      idleTime = 0; // reset to zero
});

// count minutes
function timerIncrement() {
    idleTime = idleTime + 1;
    if (idleTime > idleMax) { 
        window.location="LogOut.php";
    }
}       
</script>

Upvotes: 1

Jeff Lambert
Jeff Lambert

Reputation: 24661

I use a meta refresh element in the head section to auto-direct users to the logout page after X number of seconds. Below will automatically send a user to the logout page after 20 minutes of staying on the same page:

<meta http-equiv="refresh" content = "1200; url=http://www.site.com/user/logout">

This works, is (mostly) supported cross-browser, does not rely on JavaScript being enabled and is pretty easy to implement.

If your site has users that stay on the same page for extended periods of time (with interaction taking place through JS, for instance), this solution will not work for you. It also does not allow any JS code to be run before redirection takes place.

Upvotes: 5

Related Questions