Hello,
I am trying to keep track of how long a user is logged in for. I am kinda stumped though.
I know when a user logs in I can create a time stamp, and when a user logs out again create a time stamp and then do the difference between the time stamps. Thats the easy way to do this and I am fully aware of this method. Now what I am unaware of is how I would maintain how long a user is logged in if they close out the browser (not click on the log out button). I have an idea of how to do it? Each time a user logs in push their userId to a queue that continuously checks if the session is still registered in the DB? If the session is not valid then have the queue update the DB timestamp and do the calculation of how they were logged in? So if they close the browser, the session will be deleted from the DB (this is default behavior).
Is there any other way that is not as expensive memory wise? Since at most times we will have about 15 users logged in (its a internal sort of system). I sort of feel having 15 jobs in the queue continuously running to check if a user is logged and then possibly altering the DB would be very expensive - no?
Any tips would be great in this situation. Many thanks.
you can track him using js, for example when the user leaves the page or closes the browser, with js you can trigger an ajax call when this happens
to do this you must listen to onunload event, with jquery its implementation is super easy
$( window ).on( 'unload', function() {
$.ajax( '/user/is-leaving' );
} );
note that you not need to listen a sucess ajax event, just send the request to the server
So I'm hoping this thread will help others out there as well as myself. I also took a look into using event listerns so I found this :
Event::listen('auth.logout', function($user)
{
// Will log the user out like it normally would
// Take the time stamp from the login in time
// Then subtract it from the current time stamp
// Then with the difference add that to the total time logged in for the user
});
My only issue now is does the auth.logout event get fired when a user clicks on the close button for the browser? I guess a way to test for this would be to listen for the auth.logout event, and if it fires then have an email sent to me (just for testing purposes) to make sure that even closing the browser will end the session + fire the event.
arcollector said:
you can track him using js, for example when the user leave the page or close the browser, with js you can trigger an ajax call
to do this you must listen to onunload, with jquey its implemention is super trivial
$( window ).on( 'unload', function() { $.ajax( '/user/is-leaving' ); } );
note that you not need to listen a sucess ajax event, just send the request to the server
I'm going to take a look at if this is a cross browser solution. If it is then you just saved me a lot of time!
The answer to the second question is no. Auth has no knowledge if a user closes the window.
I would go with the ajax route as well.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community