Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. <?php
  2.  
  3. if (!isset($_REQUEST)) {
  4.         return;
  5.     }
  6.    
  7. define("SCRIPT_ROOT", dirname(__FILE__));
  8.  
  9. // number of allowed page requests for the user
  10. define("CONTROL_MAX_REQUESTS", 10);
  11. // time interval to start counting page requests (seconds)
  12. define("CONTROL_REQ_TIMEOUT", 3);
  13. // seconds to punish the user who has exceeded in doing requests
  14. define("CONTROL_BAN_TIME", 60);
  15. // writable directory to keep script data
  16. define("SCRIPT_TMP_DIR", SCRIPT_ROOT."/flood");
  17. // you don't need to edit below this line
  18. define("USER_IP", $_SERVER["REMOTE_ADDR"]);
  19. define("CONTROL_DB", SCRIPT_TMP_DIR."/ctrl");
  20. define("CONTROL_LOCK_DIR", SCRIPT_TMP_DIR."/lock");
  21. define("CONTROL_LOCK_FILE", CONTROL_LOCK_DIR."/".md5(USER_IP));
  22.  
  23. if (file_exists(CONTROL_LOCK_FILE)) {
  24.     if (time()-filemtime(CONTROL_LOCK_FILE) > CONTROL_BAN_TIME) {
  25.         // this user has complete his punishment
  26.         unlink(CONTROL_LOCK_FILE);
  27.     } else {
  28.         // too many requests
  29.         echo "<h1>DENIED</h1>";
  30.         echo "Oops, we ran into some problems.";
  31.         echo "<hr>Please try again later.</hr>";
  32.         touch(CONTROL_LOCK_FILE);
  33.         die;
  34.     }
  35. }
  36.  
  37. function antiflood_countaccess() {
  38.     // counting requests and last access time
  39.     $control = Array();
  40.  
  41.     if (file_exists(CONTROL_DB)) {
  42.         $fh = fopen(CONTROL_DB, "r");
  43.         $control = array_merge($control, unserialize(fread($fh, filesize(CONTROL_DB))));
  44.         fclose($fh);
  45.     }
  46.  
  47.     if (isset($control[USER_IP])) {
  48.         if (time()-$control[USER_IP]["t"] < CONTROL_REQ_TIMEOUT) {
  49.             $control[USER_IP]["c"]++;
  50.         } else {
  51.             $control[USER_IP]["c"] = 1;
  52.         }
  53.     } else {
  54.         $control[USER_IP]["c"] = 1;
  55.     }
  56.     $control[USER_IP]["t"] = time();
  57.  
  58.     if ($control[USER_IP]["c"] >= CONTROL_MAX_REQUESTS) {
  59.         // this user did too many requests within a very short period of time
  60.         $fh = fopen(CONTROL_LOCK_FILE, "w");
  61.         fwrite($fh, USER_IP);
  62.         fclose($fh);
  63.     }
  64.     // writing updated control table
  65.     $fh = fopen(CONTROL_DB, "w");
  66.     fwrite($fh, serialize($control));
  67.     fclose($fh);
  68. }
  69.  
  70. antiflood_countaccess();
  71.  
  72. $dblocation = "localhost";
  73. $dbname = "name";
  74. $dbuser = "use";
  75. $dbuserpass = "pass";
  76.  
  77. $link = mysqli_connect($dblocation, $dbuser, $dbuserpass, $dbname);
  78. if ( !$link ) die("error");
  79.    
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement