Count and limit the number of users in my application.

I need to implement a system to limit the number of users who can use my application at the same time. I think the best way to go is to count the number of active sessions, and then limit this.

How to count the number of active sessions. I use memcached to store my sessions, if that matters

+2
source share
3 answers

I think the easiest way is to intercept session calls open, destroyand gc(using session_set_save_handler()) and increase / decrease the value of the session counter in memcached. Sort of:

class Memcache_Save_Handler {

   private $memcached;

   public function open($save_path, $name) {
      $session_count = (int)$this->memcached->get('session_count');
      $this->memcached->set('session_count', ++$session_count);
      // rest of handling...
   }

   public function destroy($id) {
      $session_count = (int)$this->memcached->get('session_count');
      $this->memcached->set('session_count', --$session_count);      
      // rest of handling...
   }

}
+1

, PHP. , IP- (NOW() SQL). , ( MySQL):

SELECT COUNT(*) as active_users
FROM logged_users
WHERE last_action > NOW() - INTERVAL 5 MINUTE;

-.

+1

Maybe you can limit the maximum number of concurrent clients to a web server? (But this may affect the receipt of images / other static content)

+1
source

All Articles