2015-07-12 74 views
1

我正在使用CakePHP,並且我需要限制特定頁面上的頁面瀏覽量,以實現小的隨機數秒。然而,我編寫代碼,有時候,即使它不應該,阻塞觸發器也被欺騙了。按給定秒數IP限制訪問

$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分鐘。

+1

'$ credits_one'的值是多少?同時在'$ site_seconds'處設置一個整數而不是字符串:'$ site_seconds = 100;' – Stefan

+0

'$ credits_one'的值在10到120秒之間變化。當我定義我使用'$ credits_one = $ site ['Site'] ['surfsec'];',那算作一個字符串? – rgerculy

+0

我添加'$ seconds_int = intval($ credits_one);'但錯誤仍然存​​在。 – rgerculy

回答

0

我意識到我的錯誤在哪裏,我設置了時間,但是當我檢查用戶是否訪問得太快時,我檢查新計時器,並且在數字高於舊計數器的情況下,錯誤觸發。 解決這個問題的一種方法是在發佈IP時插入一個新的時間值。

謝謝@Stefan的幫助。