Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cron - update database if session expires

Tags:

php

mysql

cron

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?

like image 370
crock Avatar asked Dec 21 '25 05:12

crock


1 Answers

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

like image 153
cwallenpoole Avatar answered Dec 23 '25 19:12

cwallenpoole