Measure time the user is logged in to the system

Asked

Viewed 1,264 times

0

I need to know how much time the user spends on the system.

That is how long it remains, from the moment it makes the login, to make logout OR close/browser/disconnect PC/disconnect connection, etc.

I don’t even know how to begin. I thought through cookies or session. But I don’t know.

My intention is to keep in one BD this information. So you can generate a report of how long the user spends on the system.

  • What do you want to do with this information? Put it in the database or wouldn’t you need it? At simplest case you could pick up the timestamp at the time the user authenticates, the timestamp at the time of logout and check the total time logged in. The close tab/browser event can also catch you, but note that this does not cause the user to be "dropped" unless you are forcing this into your code. For loss of connection you could make a Polling through an ajax function...

  • ... and when that function does not get more result you consider that there was logout. But see that this way the code would be on the client side and depending on how your system is, would not have connection to the database and everything. I think the best thing would be to try to explain the final idea with this, so that someone can help in the best way.

  • @Adrianoleal, my question is how will I store in the bank the moment of logout? If he clicks on the logout option, great but what if he just closes the tab, or browser...

  • @Adrianoleal, I believe Pope Charlie’s answer!

  • Yes, I also found Papa Charlie’s answer very good, @Diegozanardo. It does not cover everything you want, for example, in loss of connection requests would not work and you would lose that control, but if we take into account that connection losses will occur a few times there would be no problem. Ah, I also find it costly to spend every user session communicating with the bank every minute, but it’s an alternative, so it varies from case to case.

  • Another thing, updating the information only when loading the page may not be ideal, if your pages have large contents (I do not know the content of your system) the user could stay on the page for a long time, for example reading a text or watching a video and you only recorded the date and time of when the user entered the page. The perfect scenario would be to make these requests in the tab/browser onClose event as well, but I’ve never done this in JS.

Show 1 more comment

2 answers

3


From the description you gave, I imagine you do not save session, much less cookie, and leaving the site should log in again. So I present a simple example of 2 tables.


USERS
ID | NOME         | SENHA | EMAIL...
1  | Papa Charlie | ***** | [email protected]

USER-LOGIN
ID | ID_USER | LOGIN_TIME       | LAST_LOGIN
1  | 1       | 07/09/2014 11:00 | 07/09/2014 12:00 // login ontem
2  | 1       | 08/09/2014 13:00 | 08/09/2014 17:00 // login hoje

1) When the user logs in to the site, record in your DB USER-LOGIN the last updated time. Each login enters a record in USER-LOGIN, and every request is updated.

In the example of the tables above, yesterday’s login lasted 1 hour (11 to 12) and today’s login lasted 4 hours (13 to 17)

2) The next step would be to send a request via js to update the field.

setTimeout(function(){
    $.get("check_user.php");
} 60000 );

Every minute the page check_user.php will receive user data and update the field LAST_LOGIN

  • I am sending a request to the server and by changing the BD, can not cause slowness and overload of BD and server?

  • You can update every page load, but in that case you would have an "approximate" logout time.

1

Maybe a code like this in Login:

//if(session["LogadoDesde"]==null)
    session["LogadoDesde"] = DateTime.Now();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.