Php – Login, logout and duration time in php and thesql

duration, login, logout, mysql, php

I would like to store the login, logout and duration time in database.

  • The login time is created when the user is authenticated(successfully logged in)
  • The logout time is created when the user clicks the logout button
  • The duration is logout – login time. (logout minus login)

But the problem is, what if the user didnt click the logout button. Here are the situations:

  • Internet loss
  • Close the browser/tab. (I need this must use javascript, but i donnu how to do it, any idea?)


I forgot to add something to the question, the program is a full flash program, there is no navigation to other page. Only 1 page

Best Solution

You can't rely on receiving an event for the user logging out, if they simply close their browser, or disappear from the internet.

In this case you'll have to have a session timeout of some kind, and record the logout when your app realises their session is too old.

If this is a real requirement, then I'd say you need a "cron" job monitoring the sessions for timeout. When a session has timed out, if the were logged on, it then records a "logout" event for that user.

Note that you can't use (for example) ASPNET's Session_End event, because that won't be reliably called either (for example if the server process restarts).

Another option is to add the logout time next time that user logs on - when they log on, you check for old sessions and assume that any which weren't closed lasted for a fixed amount of time since the last page hit.

That's really all you can do.