I have got a "users" table in mysql database, in one field I keep the info if the user is online.
Everything works fine apart of:
-if some user closes the browser, his online status doesn't change to offline.
I have been told that I could do that with cron, but I don't know anything about it.
I can save the user id in his session and make the php function to update the db.
But... I have no idea how to make the cron run that php (with the id from the session)
How easy is it to do?
It is fairly simple. This might be in your PHP file (call it update.php).
<?php
/*connect to mysql as per normal*/
// log users off (hopefully, you're using enum here, but for an example...)
mysql_query( 'UPDATE USERS SET STATUS = \'LOGGED_OFF\' '.
// whose last action was more than 14400 seconds ago (4 hours)
'WHERE (CURRENT_TIMESTAMP() - TIMESTAMP(LAST_ACTION)) > 14400' );
?>
Then, open crontab:
crontab -e
and add this line to run the tast every 15 minutes (change the number 15 to another number if you like):
*/15 * * * * /path/to/update.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With