Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warn the user before session timeout

I have a web application implemented in Spring MVC, JSP.
Default session timeout is defined in web.xml is 30 min.

if user is idle for 25 mins than I need to show a popup to user with message that Your session is going to be end by 5 min, Please click OK to continue.

Do we can achieve this using JavaScript, jquery or any other approach?

Please suggest.

like image 763
user3509911 Avatar asked May 29 '14 09:05

user3509911


People also ask

How do I alert someone before session timeout?

1 Answer. Show activity on this post. First things first: When you want to warn the user 5 minutes before the 15 minutes timeout, the warning shoud be displayed at 10 minutes after page load. setTimeout( function() { alert("Your session will expire in 5 minutes."); }, 10*60*1000);

What can cause session timeout?

Possible reasons for seeing the session expired messageUser has been inactive for more than the specified time and the session has timed out. User has been disconnected from the internet mid-session. User has logged in on a different machine while the initial session is still active.

What happens when session timeout?

Session timeout represents the event occuring when a user does not perform any action on a web site during an interval (defined by a web server). The event, on the server side, changes the status of the user session to 'invalid' (ie.


3 Answers

Try this code. Hopefully it will solve your problem. Thanks

var cookieName = 'sessionMsg';
var message = 'Your session is going to be end by 5 min, Please click OK to continue';

function getCookie(name)
{
    var name = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++)
    {
        var c = ca[i].trim();
        if (c.indexOf(name)==0) return c.substring(name.length,c.length);
    }
    return "";
}

function setSessionPrompt() {
    var timeout = getCookie(cookieName) - new Date().getTime();
    setTimeout(function(){
        if (new Date().getTime() < getCookie(cookieName)) {
            setSessionPrompt();
        } else {
            if(confirm(message)) {
                // do your action here
            }
        }
    }, timeout);
}

setSessionPrompt();
like image 123
Mohit Avatar answered Oct 19 '22 19:10

Mohit


You can use like follows: I am using that in a jsp page like header.jsp. It works fine for me. I am using spring, jsp and jquery.

javascript:

<code>
<script type="text/javascript">
    //var recall = true;
    function loadDialog() { 
            var sessionAlive = ${pageContext.session.maxInactiveInterval};          
            var notifyBefore = 30; // Give client 15 seconds to choose.
            setTimeout(function() {       
                $(function() {
                    $( "#dialog" ).dialog({
                        autoOpen: true,
                        maxWidth:400,
                        maxHeight: 200,
                        width: 400,
                        height: 200,
                        modal: true,
                        open: function(event, ui) {
                            setTimeout(function(){
                                $('#dialog').dialog('close'); 
                            }, notifyBefore * 1000);
                        },
                        buttons: {
                        "Yes": function() {  
                             $.get("/about", function(data,status){
                               // alert("Data: " + data + "\nStatus: " + status);
                              });                             
                            $('#dialog').dialog("close");
                            loadDialog();
                        },
                        "No": function() {  
                            $('#dialog').dialog("close");
                        }
                       },
                       close: function() {}
                    });
                  });
            }, (sessionAlive - notifyBefore) * 1000);
    };

    loadDialog(); 
</script>

HTML Div:

<div id="dialog" title="Session Timeout Info" style="display:none">
  <p>
    Your session will be timeout within 30 seconds. Do you want to continue session?  
  </p>
</div> 
</code>
like image 39
Md. Kamruzzaman Avatar answered Oct 19 '22 18:10

Md. Kamruzzaman


You could send a cookie with the remaining time (in milliseconds) on every request. Then you can use setTimeout as suggested but test the value again when the timeout-function is executed. If the value has changed you can reset the timeout. Since the cookie is set for the whole domain it will always be correct even for ajax or other tabs.

var cookieName = 'sessionMsg';
var message = 'Your session is going to be end by 5 min, Please click OK to continue';

function getCookie(name)
{
    var name = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++)
    {
        var c = ca[i].trim();
        if (c.indexOf(name)==0) return c.substring(name.length,c.length);
    }
    return "";
}

function setSessionPrompt() {
    var timeout = getCookie(cookieName) - new Date().getTime();
    setTimeout(function(){
        if (new Date().getTime() < getCookie(cookieName)) {
            setSessionPrompt();
        } else {
            if(confirm(message)) {
                // do your action here
            }
        }
    }, timeout);
}

setSessionPrompt();
like image 2
flec Avatar answered Oct 19 '22 17:10

flec