Sep 1 / Phil

idleTimeout – jQuery Idle Session Auto Timeout with Prompt

Update: I just added a CC license (see below) and renamed this project idleTimeout

I just finished the first draft of idleTimeout -  jQuery auto prompt / logout / session expire plugin.  It is fairly simple, but seems to work well for my needs.

session-timeout

Installation is simple: grab a copy of jquery-idleTimeout.js , include it in your page, and activate it.

/** Run with defaults **/
  $(document).ready(function(){
    $(document).idleTimeout();
  });
/** With Optional Overrides **/
  $(document).ready(function(){
    $(document).idleTimeout({
      inactivity: 30000,
      noconfirm: 10000,
      sessionAlive: 10000
    });
  });

Note:  This uses the jQuery UI dialog, and UI themes.  I am working on a non UI version – but it may be a while

I have an active demo here – it is running on a 30 second timer for the logout, and if you open firebug you will see the keep alive firing every 10 seconds.

The plugin has a few configuration items so you can customize it for your own needs…

  • inactivity: 1200000 //20 Minute default (how long before showing the notice)
  • sessionAlive: 300000, //5 minutes default how often to hit alive_url, we use for our ajax interfaces where the page doesn’t change very often. This helps to prevent the logout screen of your app appearing in ajax callbacks.  If you set this to false it won’t send off.
  • alive_url: ‘/path/to/your/imHere/url’, //send alive ping to this url
  • redirect_url: ‘/js_sandbox/’, //Where to go when log out
  • click_reset: true, //Reset timeout on clicks (for ajax interface) – resets the sessionAlive timer, so we are not hitting up your app with alive_url if we just did an ajax call for another  reason.
  • logout_url: ‘/js_sandbox/timedLogout/index.html’ //logout before redirect (url so you can completely destroy the session before redirecting to login screen)

Creative Commons License
idleTimeout by Phillip Palmieri is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.

Please let me know if you are using it, or if you have any ideas to make it better!

Thanks,
Phil

36 Comments

leave a comment
  1. yupi / Sep 3 2009

    Nice. Thank you! Overall, exactly what I was looking for!!

    Two questions…

    I am having is understanding the use of sessionAlive. What kind of function I should put in alive_url?

    Also

    For redirect function
    var redirect = function()
    {
    if(opts.logout_url)
    {
    $.get(opts.alive_url);
    }
    window.location.href = opts.redirect_url;
    }

    may be better to be

    var redirect = function()
    {
    if(opts.logout_url)
    {
    $.get(opts.logout_url);
    }
    window.location.href = opts.redirect_url;
    }

    ?

    • Phil / Sep 3 2009

      Your right! – typo on my part, that should be hitting up the logout_url before the redirect. – updating the js file now.

      the aliveUrl is a url that they system will hit every X minutes to keep your serverside session alive.. like site.com/still_here – this way you can update your server session and make sure next tim eyou click you dont a login box in your return.

  2. yupi / Sep 3 2009

    Ah! Thank you so much for your quick response. alivrUrl makes sense now.
    I appreciate you sharing this script! :)

  3. Ian / Sep 10 2009

    This is awesome – worked out of the box for me in an admin tool situation. You may want to fix your example because it has a permissions problem.. cheers!

    • Phil / Sep 10 2009

      @ian thanks – it was still setup for my local development setup… i just switched it to live, and also shortened the time on the demo.

      • Ian / Sep 11 2009

        Correct me if I’m wrong but looking at it again – this code seems to time out the user after out after 20 minutes even if he/she is busily tapping away at an ajax interface. What is needed is some sort of response from the alive_url that the session is still valid – but this would give no warning of the session ending – or a way to reset the inactivity timer when an ajax call is made elsewhere on the page…

  4. Keith / Sep 13 2009

    I am looking for a script that I can use with a PHP/MySQL website. I do run other jQuery stuff however. Will this script unset the session variables? Thanks!

  5. Keith / Sep 13 2009

    My website is predominantly PHP/MySQL although I use some jQuery. Will this script unset the PHP session variables?

    • Phil / Sep 13 2009

      It shouldn’t effect your backend at all

      • Keith / Sep 13 2009

        OK so how can it log someone out if it doesn’t unset the PHP session variables?

  6. Keith / Sep 14 2009

    Phil, I can’t seem to get this working. I have saved the js file and included it on a test page but nothing is happening. This is how the variable group looks…

    var defaults = {
    inactivity: 30000, //20 Minutes
    noconfirm: 10000, //10 Seconds
    sessionAlive: 10000, //30 seconds
    redirect_url: ‘../../index.php’,
    click_reset: true,
    alive_url: ‘../../menu.php’,
    logout_url: ‘../../endSession.php’
    }

    any problem with this?

    • Phil / Sep 14 2009

      Hey,

      As for customizing, you can tweak the copy in the plugin, but i pretty much left it 100% up to your UI theme.

      I also updated the post with instructions on activating the plugin..

      Phil

      • Keith / Sep 14 2009

        Phil, I have it working now…almost. The logout appears to work but I get a 404 message saying that it can’t find the redirect page (index.php). I have tried writing the URLs in full, in part and as root-relative. Are the URL paths relative to the location of the js file?

  7. Keith / Sep 14 2009

    Update – the logout is working well, but for reasons unknown the logout warning dialogue simply doesn\’t appear!

    • Phil / Sep 15 2009

      do you have it up in a publicly accessible place? – shoot me an email, and i’ll see if i can help you out.

      • Keith / Sep 21 2009

        Phil,
        apologies for delay in replying, thanks for following through. As you can see the website I am trying to incorporate your script into is a secure company intranet. The nature of the site contents is such that users are very likely to dip in and out of using it but are likely to keep logged in so I am keen to get things moving. Perhaps we could take this off-line and I’ll provide you with a login.
        Thanks,
        Keith

  8. Keith / Oct 14 2009

    Phil, I am now using the script in a live environment. However, although the script redirects to the login page it does not appear to be redirecting to the logout page first, where I have all the PHP code to unset the session variables.

  9. Michael / Oct 23 2009

    Instead of using a true false for click reset how about using a list of events…

    var defaults = {
    inactivity : 1200000, //20 Minutes
    noconfirm : 90000, //90 Seconds
    sessionAlive : 600000, //10 Minutes
    redirect_url : ‘/’,
    alive_url : ‘/’,
    logout_url : ‘/’,
    events : ‘keydown mousedown mousemove’
    }

    then instead of:
    if(opts.click_reset) {
    $(document).bind(‘click’, start_liveTimeout);
    }

    use:
    //assign appropriate event handlers for resetting timeout
    $(document).bind($.trim((opts.events + ‘ ‘).split(‘ ‘).join(‘ ‘)), start_liveTimeout);

  10. Michael / Oct 23 2009

    How about a way to check on the status of a timeout?

  11. Michael / Oct 23 2009

    Maybe I’m missing something but how about a way to submit contributions other than posting them here?

    • Phil / Oct 23 2009

      If i keep getting as much traffic on it, i will move it to google code.

  12. Thomas / Oct 29 2009

    Just wondering what license this is released under. For our purposes GPL or the like might be necessary. Works like a charm in our code BTW. This solves a problem we have been stewing over for about a month. Thanks.

    • Phil / Oct 29 2009

      I am registering it as Creative Commons Share Alike

  13. Mark / Jan 6 2010

    Does this not work in aspx pages? I tried using this but it works if i implement this in htm page. When i put this in aspx page, it doest seem to work. Am i doing something wrong here?

    • Phil / Jan 7 2010

      It’s all client-side, so it shouldn’t matter what you are using on the server. What do yo mean when you say put it ‘in’ an aspx page?

  14. Dan / Jan 30 2010

    I think there is a problem with this.

    1. Session timeout=20 minutes
    2. idleTimeout shows when 5 minutes is left.

    Now, if I just move the mouse or type some text in a box it does not reset the session on the server. so…10 minutes later I leave the desk, session has 10 minutes left but the idleTimout popup will now show for another 15 minutes. Meaning the session will have already expired.

    What do I do?

    • Phil / Feb 23 2010

      Hi,

      Not sure i follow you 100%.. you are saying the click_reset is not happening on each click? or it is working, but your screen isnt reloading (meaning the screen stays up, but your session is expired)?

      • Dan / Feb 23 2010

        the click_reset is not relevant with this issue because all the user did in this scenario is move the mouse which reset the jquery timer to another 20 minutes, but the move of the mouse did not reset the server, so after 5 minutes the server times out but no new notification.

        Lets say I just keep moving the mouse on the web site, no clicks, postbacks, nothing else. The moving of the mouse resets the jquery timer, that is what I am refering to because the moving of the mouse does not reset the session on the server. So, say the user spends 10 minutes filling out a form then spends another 10 minutes just looking it over, the last 10 minutes will not reset the server so the count down will be set to 20 minutes but the server will have only 10 minutes left in it’s session. user go away for another 10 minutes, the session has timedout but no notification(not for another 10 minutes)

        Not sure that makes any sense at all but thats what is going on, I have since changed the way I use the code so I have a workaround where I do not use mouse or click events.

  15. Andrew Donald / Feb 20 2010

    Hi – Can you clarify the following pleas:-
    I don\’t get alive_url and SessionAlive?

    Our production web server times-out after 60 mins, which means if you open a page and don\’t do a submit within 60 mins, you\’re logged out. I want to show a dialogbox at 40 mins of inactivity, so I set parameter inactivity to 2400000. After the dialogbox shows, I want it to stay up for 20 mins, so I set noconfirm to 20 mins = 1200000. All clear so far. What I don\’t get is, how can clicking button \’Stay logged in\’ in the dialogbox at at 58 mins have the desired effect of not loggin me out 2 minutes later? Does pinging keep a session alive?

  16. Andrew Donald / Feb 23 2010

    Why have you removed my comment?

    Your instructions are not clear. I don’t want to trawl through the code. That’s the whole point fo jQuery!

    • Phil / Feb 23 2010

      Hi Andrew, I did not remove your comment… I did however, Not post the duplicate posting.. I did also take a day or two to approve then pending post as well.

      I am sorry my documentation is not your standards, however i don’t equate 100 lines (including comments btw) trawling… Plus, I do believe the whole point of jQuery is that it is open source, and well… community driven/powered… So, no i do not have a staff of customer service staff on hand to explain everything to you.

      To answer your question, hopefully more clear than in the post

      • inactivity : how long before popping up
      • sessionAlive: how often to do an async call to let your server side know the session should be kept alive
      • alive_url : the url you want the async request to go to (you need to write a handler for this on your server

      When you click ’stay logged in’ it fires of because alive_url is optionally bound (with click_reset) to click event and also because it sends alive_url a request when you click stay logged in.

      I hope this helps you out.

  17. vishwajeet / Mar 3 2010

    after including jquery-idleTimeout.js file
    i’m getting error like this
    jQuery is not defined
    })(jQuery);

    waiting for reply

    • Phil / Mar 3 2010

      Might be a dumb question, but do you have the jQuery library loaded?

  18. Phil / Sep 11 2009

    @ian, i think i follow you, but….

    It’s not tied to the server session at all, the ping is just so that your backend can know the user is still there (which should keep the session alive on its own) this prevents the system from ending the user session and returning a login/end screen on the ajax callback. This JS is meant to just force the user to push to the logout if they don’t interact with the front-end. The click_reset resets the 20 minute timer on each click and, if the user is clicking, that will keep the system session alive as well.

  19. Phil / Sep 13 2009

    All it is doing is hitting a logout URL that you set to match the equivelant of a normal logout link… Like account/logout – this is where your server side (php in your case) would do the session destroy so that when the script redirect the user to a login page they are back to square one.

  20. Keith / Sep 14 2009

    Phil, many thanks. It\’s a great script, any idea when you might be enabling some customization on the UI?

Leave a Comment

Security Code: