Advertisement
reenadak

flood checker

Feb 20th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.84 KB | None | 0 0
  1.     /*
  2.     - Flood Checker
  3.     - Checks that the user is not spamming or "flooding" the database.
  4.    
  5.     $interval     - How many seconds between each post from this IP address
  6.     $ip           - The IP to flood check, default is the user's IP
  7.     */
  8.    
  9.     public function floodCheck($interval = 15, $ip = null) {
  10.         $success = true;
  11.        
  12.         if (file_exists("Floodcheck.txt")) {
  13.             $file = fopen("Floodcheck.txt", "r");
  14.  
  15.             if ($ip == null)
  16.                 $ip = $_SERVER['REMOTE_ADDR'];
  17.            
  18.             // sort each line addresses
  19.             while (($line = fgets($file)) !== false) {
  20.                 $line = trim($line);
  21.                 $flood = explode(",", $line);  
  22.  
  23.                 // check if ip matches ours
  24.                 if ($flood[0] == $ip) {
  25.                     // check if the flood check is successful
  26.                     if ((time() - $flood[1]) < $interval)
  27.                         $success = false;
  28.                 }
  29.             }
  30.            
  31.             fclose($file);
  32.         }
  33.        
  34.         return $success;
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement