限制给定秒数的 IP 访问

Limit visit by IP for given seconds

我正在使用 CakePHP,我需要将特定页面上的页面浏览量限制在一小段随机秒数内。然而,我编写代码,有时,即使不应该,阻塞触发器也被欺骗了。

$site_seconds = "100";          
$get15 = $site_seconds * 5 / 100;
$ip_session = $credits_one - $get15;
$ip_session_time = round($ip_session);

我使用它是为了得到比实际访问时间更少的数字,它是为了避免像我这样的错误而创建的,并不是很有帮助。 因此,如果我们有 100 秒,我们将计算 95 秒。

    $conditions_check = array(
        'date >=' => date('Y-m-d H:i:s', strtotime("-$ip_session_time SECONDS")),
        'user' => $user_id,
        'value1' => $ip,
        'type' => '3'
    );

       /// we check if the condition has any rows 
        if ($this->Log->hasAny($conditions_check)){ 
           // If found I send a private message to the user
             $this->Log->set('date', date('Y-m-d H:i:s')); // time here is only for ordering when displayed to user
             $this->Log->set('user', $user_id);
             $this->Log->set('value1', $ip);
             $this->Log->set('value2', "No credits received for this visit. To many conditions from same IP.");
             $this->Log->set('value3', "1");
             $this->Log->set('type', '4');
             $this->Log->save();
            }else{
           // The code runs
            }

我尝试使用 strtotime(*now*); 但更糟糕的是,错误每次都会出现,使用此代码,可能每 2 分钟一次。

我意识到我的错误在哪里,我设置了时间,但是当我检查用户访问速度是否太快时,我检查了新的计时器,如果数字大于旧的,就会触发错误. 修复它的一种方法是插入一个新的时间值,即应该释放 IP 的时间。

感谢@Stefan 的帮助。