Need a tracking PHP Script – “how long people stay on the website?”

I want to know, is there a way to track how long anyone has been logged into website and then insert it up for each time they have logged on.

In case, user a logs in for 30 minutes, then later comes back and logs in for an hour, then later comes back and logs in for 50 minutes,

Add it up equal 2 hours 20 minutes. Is this possible?

Thanks

If you’re storing the sessions in the db then you could have a field in the session table which would be a timestamp (set when the user logs in) and then when either the user logs out or their session expires, the elapsed time between when they logged on and when they logged off would be calculated and added to the relevant field in the user table

You’ll need a cronjob (or equivilant) to do the end-time though, since there isnt a callback on session expiry.

Just because a session expires after ‘x’ hours doesn’t mean that the person was actually on the site all that time. They may have only been there a few seconds and then forgot to log off leaving their session to expire based on settings on the server.

If they have javaScript enabled then you could set up a script on the page that calls the server at regular intervals to advise that they are still on the site. You would then know that they had left when more than that amount of time passed without the script making another call.