计算并限制我的应用程序的用户数量

发布于 2024-09-30 12:13:51 字数 123 浏览 0 评论 0原文

我需要实现一个系统来限制可以同时使用我的应用程序的用户数量。我认为最好的方法是计算当前活动的会话数量,然后据此进行限制。

如何计算当前活动会话的数量。我正在使用 memcached 来存储我的会话(如果这有影响的话)

I need to implement a system to limit the number of users that can concurrently use my app. I am thinking the best way to go is to count the number of sessions currently active and then limit based on that.

How can I count the number of sessions currently active. I am using memcached to store my sessions if that makes a difference

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最单纯的乌龟 2024-10-07 12:13:51

我认为最简单的方法是拦截会话的 opendestroygc 回调(使用 session_set_save_handler()) 并增加/减少 memcached 中的会话计数值。像这样的东西:

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...
   }

}

I think the easiest way is to intercept the session's open, destroy and gc callbacks (using session_set_save_handler()) and increment/decrement a session count value within memcached. Something like:

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...
   }

}
一身仙ぐ女味 2024-10-07 12:13:51

我不知道有什么方法可以在 PHP 中计算给定时间的每个活动会话。为此,我一直使用数据库,在其中存储 IP 地址和当前日期(NOW() SQL 函数)。然后,我可以执行这样的查询(MySQL语法):

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

然后您可以选择显示该页面或禁止用户查看该网站。

I don't know any way to count every active sessions at a given time in PHP. I've always used a database for this, where I'd store the IP address and the current date (NOW() SQL function). Then, I you can do a query like this (MySQL syntax) :

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

You can then choose to display the page or forbid the user to see the website.

心清如水 2024-10-07 12:13:51

也许你的网络服务器可以限制并发客户端的最大数量? (但这可能会影响获取图像/其他静态内容)

May be you web-server can limit maximum number of concurrent clients? (But this can affect fetching images/other static content)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文