简体   繁体   中英

How to restrict a user to login in only one tab/window using java and javascript?

My requirement is to give access like one login per user, For that I have updated the login Status to true in db when user login, and false when user logout. But the problem is when user close the window without logout.

To handle window close I have implemented the following js code

 var validNavigation = false;

 function wireUpEvents() {

  var dont_confirm_leave = 0; 
   var leave_message = 'You sure you want to leave?'
  function goodbye(e) {
    if (!validNavigation) {
     if (dont_confirm_leave!==1) {
         if(!e) e = window.event;
       //e.cancelBubble is supported by IE - this will kill the bubbling process.
       e.cancelBubble = true;
        e.returnValue = leave_message;
       //e.stopPropagation works in Firefox.
        if (e.stopPropagation) {
         e.stopPropagation();
         e.preventDefault();
          }
        //return works for Chrome and Safari
        return leave_message;
      }

      window.location = "logout.jsp";
    }
  }
  window.onbeforeunload=goodbye;

   // Attach the event keypress to exclude the F5 refresh
  $(document).bind('keypress', function(e) {
    if (e.keyCode == 116){
      validNavigation = true;
    }
  });

  // Attach the event click for all links in the page
  $("a").bind("click", function() {
    validNavigation = true;
  });

  // Attach the event submit for all forms in the page
  $("form").bind("submit", function() {
    validNavigation = true;
  });

   // Attach the event click for all inputs in the page
  $("input[type=submit]").bind("click", function() {
    validNavigation = true;
  });

  // Attach the event click for all inputs in the page
 $("input[type='button']").bind("click", function() {
   validNavigation = true;
 });

}

// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
 wireUpEvents();
});

Here it works for window close, means if user close the window it goes to logout page, but problem is it was going to logout page when user reloads the page.

So I need bind the reload event also like the above js code for f5, submit and anchor tags.

Pleae help me in this regard.

Thanks in Advance...

Two things came into my mind:

First of all the fact that the user is logged in, doesnt mean that theres any activity going on, some people leave there pc turned on with the browser running.

Second is that even if the user closes a page it doesnt necesserly means that he/she wanted to logout and its especially true if the user use two tab to navigate on your site.

So instead of trying to check when they close the tab I suggest considering the idea of logging them out only when they login from somewhere else and/or setting up a timer that automatically log them out after a certain amount of inactivity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM