Check that users are online

I have a PHP variable $ _SESSION with username. I have a MYSQL database with a unique field for the username. How can I keep db up to date with the names of online users and also delete them when they leave. Is there a way to do this using jQuery.ajax?

+3
source share
4 answers

As I do this, a special handler pings using AJAX periodically (once every 30 seconds). This is a lot of requests, so instead of updating the online user table every time with the last visit, I update memcache. I only update the database record when it is 5 minutes behind the memcache record (you can choose a longer or shorter delay depending on the load). The cron job then deletes obsolete entries from the online user table.

To check if a particular user is online, I just check his memcache entry. Error no more than 30 seconds. The database does not exceed 5 minutes, so the results from the database are also quite accurate.

I also use these periodic queries to input events to the user.

0
source

, . , , 5 , cron, - , 5 .

, -. , 2 .

0

, , - . , - last_login last_seen , .

, , . , last_seen , , . (15 , 30 ?) , .

ajax , , , - last_logout, :

  • /.
  • javascript.
  • .
0

- MySQL MEMORY, / ( ). , timestamp ( /). , userID , ( ) cron, , , 5 . , cron. ? ( ), , .

0

Source: https://habr.com/ru/post/1793291/


All Articles