Php 限制IP在给定秒数内的访问

Php 限制IP在给定秒数内的访问,php,mysql,cakephp,time,Php,Mysql,Cakephp,Time,我使用的是CakePHP,我需要将特定页面上的页面浏览量限制为一个小的随机秒数。我编写代码,但是,有时,即使不应该,阻塞触发器也会被欺骗 $site_seconds = "100"; $get15 = $site_seconds * 5 / 100; $ip_session = $credits_one - $get15; $ip_session_time = round($ip_session); 我使用这个,所以我得到了一个比实际访问时间低的数字,它是为了避免像我这样的

我使用的是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
            }

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

我意识到我的错误在哪里,我设置了时间,但当我检查用户是否访问过快时,我会使用新计时器进行检查,如果数字高于旧计时器,则会触发错误。 解决这个问题的一种方法是在应该释放IP时插入一个新的时间值


感谢@Stefan的帮助。

我意识到我的错误在哪里,我设置了时间,但当我检查用户是否访问过快时,我会使用新计时器进行检查,如果数字高于旧计时器,则会触发错误。 解决这个问题的一种方法是在应该释放IP时插入一个新的时间值


感谢@Stefan的帮助。

积分$credits\u one的价值是多少?还可以将整数设置为
$site\u seconds
而不是字符串:
$site\u seconds=100积分值在10到120秒之间变化。当我定义时,我使用
$credits\u one=$site['site']['surfsec'],这算是一个字符串吗?我加上
$seconds\u int=intval($credits\u one)
但错误仍然存在。由于任何原因,您的脚本没有被调用两次?
$credits\u one
的值是多少?还可以将整数设置为
$site\u seconds
而不是字符串:
$site\u seconds=100积分值在10到120秒之间变化。当我定义时,我使用
$credits\u one=$site['site']['surfsec'],这算是一个字符串吗?我加上
$seconds\u int=intval($credits\u one)但错误仍然存在。您的脚本没有因为任何原因被调用两次?