Reputation: 1691
I have a site I am developing and it has a table that stores a log of all user activity. I am storing logins... and once a user is logged in an AJAX call (every minute) adds a new record to the log table with an updated datetime for that user. I am printing this log to the screen and want to echo when a user logs in and out without being too inefficient with the code. Not sure if this should be done in the SQL query or through PHP...
What I want to print out:
Log table fields would be id, user, datetime
Any help would be appreciated!
Upvotes: 2
Views: 326
Reputation: 3523
As far as I know there's no very efficient way to do this using only PHP and MySQL. What you could do is use a query like "SELECT * FROM users WHERE lastseen<" . $oneMinuteFromNow
.
To make this a bit more efficient you could add the result of this query in another table that makes entries for log in/out actions. That way you can filter the users given as result in the previous function.
If you want to go beyond PHP you can set a cronjob to run every minute or so, which checks who has been inactive for more than a minute, thus being offline. The code in the cronjob can then put an entry in the MySQL table to show the user logging off.
Upvotes: 1