-1

I have created web application that handle user login with it and update logout time when user logout from application, but when user close web browser directly or system gone shutdown with some problem, i am not able to update user logout time.

Please give any possible way to update user logout time on such circumstances.

Narayan
  • 6,031
  • 3
  • 41
  • 45
Vijay
  • 8,131
  • 11
  • 43
  • 69

8 Answers8

4

Assuming javascript on client side as it is a webapp.

In such cases you should send a request in browser close event

window.onbeforeunload = function(event) {

   //send  request to server .

}

Prefer to read :want to detect browser close event?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
3

May the following steps help you to update logout time.

1.Keep updating a timestamp variable in session for each request.
2.During session time out get the variable value (which holds when user accessed at last) and update in logout record.

This could help without depending the browser to send logout request.

vels4j
  • 11,208
  • 5
  • 38
  • 63
2

You can create a class which implements HttpSessionListener and annotated @WebListener() like this:

@WebListener()
public class MyListener implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent se) {

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
         Date lougOutDate=new java.util.Date();
    }
}

in sessionDestroyed method you retrieve the date of disconnection

Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
2

try this code to implement

        window.onbeforeunload = function(event) {
            var isOK = confirm("Are you sure to Leave this Page?");
            if(isOK)
            {
                // try ajax for update your table   
            }

        }
Vijay
  • 8,131
  • 11
  • 43
  • 69
Panchotiya Vipul
  • 1,226
  • 5
  • 17
  • 42
  • thanks this code work fine for me... – Vijay Jul 09 '13 at 12:29
  • this is not true anymore. Since 25 May 2011, the HTML5 specification states that calls to window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event. source: https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload?redirectlocale=en-US&redirectslug=DOM%2FMozilla_event_reference%2Fbeforeunload – Roy Lee May 20 '16 at 08:15
1

Why don't you use a TimeOut ?

There is several solutions :

  • Timeout
  • Implement HTTP COOKIE. Check the link below

http://en.wikipedia.org/wiki/HTTP_cookie

In your specific case it should be Session Cookie

Session cookie A user's session cookie[14] (also known as an in-memory cookie or transient cookie) for a website exists in temporary memory only while the user is reading and navigating the website. When an expiry date or validity interval is not set at cookie creation time, a session cookie is created. Web browsers normally delete session cookies when the user closes the browser.[15][16]

  • Do the stuff with Javascript as suggested Hope it's help :)
Rollyng
  • 1,387
  • 2
  • 12
  • 18
0

You could try to write some Javascript that sends an "User logging out" message to the server. This code should be triggered using $.unload() (if you're using jQuery) or binding to the native unloadevent of the browser.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
0

There is no way of precisely getting the logout time in such circumstances, as this might be for example caused by internet connection loss.

Some possibilities:

  1. Send an ajax request on document unload to notify your server (as suggested by @Baadshah and @mthmulders)
  2. Add a session timeout listener on the server to set the logout time when the session times out - this way even if the ajax doesn't get to the server you will know that the user logged out during the last few minutes (depending on the session duration)
dratewka
  • 2,104
  • 14
  • 15
-1

Use the event beforeunload with jquery on your page.

The beforeunload event fires whenever the user leaves your page for any reason.

For example, it will be fired if the user submits a form, clicks a link, closes the window (or tab), or goes to a new page using the address bar, search box, or a bookmark.

You could exclude form submissions and hyperlinks (except from other frames) with the following code:

var inFormOrLink = false;
        $(document).on('click','a', function() { inFormOrLink = true; });
        $(document).bind('submit','form', function() { inFormOrLink = true; });

        $(window).on('beforeunload',document, function(eventObject) {
            var returnValue = undefined;
            if (inFormOrLink == false) {
                //do your action

            }

        }); 

EDIT: Answer found here: How to capture the browser window close event?

Community
  • 1
  • 1
TroyAndAbed
  • 301
  • 1
  • 10