Advertisement
AlexanderNorup

Antispammer

Sep 18th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.36 KB | None | 0 0
  1. function checkAntispam(){
  2.     $antiSpamTime = 2; //How fast does to connections have to be to be considered spam?
  3.     $antiSpamMaxScore = 1; //Number of times you can spam untill getting temporaly banned
  4.     $antiSpamMultiplier = 5; //Set a multiplier to figure out ban time. Ban time is calculated like this: bantime=multiplier*number of bans
  5.     $jsonfile = "./antispam.json"; //Data file for storing
  6.     $json = NULL;
  7.     try{
  8.         $json = file_get_contents($jsonfile);
  9.         $json = json_decode($json, true);
  10.         if(isset($json[$_SERVER['REMOTE_ADDR']])){
  11.             //We have seen the user before
  12.             if($json[$_SERVER['REMOTE_ADDR']]["blockedUntil"] > time()){ // User is banned
  13.                 return false;
  14.             }
  15.             if(time()-$json[$_SERVER['REMOTE_ADDR']]["lastConnect"] < $antiSpamTime){ //If user has connected within x seconds again, then increment spamscore
  16.                 $json[$_SERVER['REMOTE_ADDR']]["spamScore"] += 1;
  17.                 $json[$_SERVER['REMOTE_ADDR']]["lastConnect"] = time();
  18.             }else{ //Hvis ikke, så ingen spamscore.
  19.                 $json[$_SERVER['REMOTE_ADDR']]["lastConnect"] = time();
  20.                 $json[$_SERVER['REMOTE_ADDR']]["spamScore"] = 0;
  21.                 if($json[$_SERVER['REMOTE_ADDR']]["banCount"] > $antiSpamMaxScore){
  22.                     $json[$_SERVER['REMOTE_ADDR']]["banCount"] = $antiSpamMaxScore; //Max score!
  23.                 }
  24.             }
  25.             if($json[$_SERVER['REMOTE_ADDR']]["spamScore"] >= $antiSpamMaxScore){ //Brugerens spamscore er for høj
  26.                 $json[$_SERVER['REMOTE_ADDR']]["banCount"] = $json[$_SERVER['REMOTE_ADDR']]["banCount"] + 1;
  27.                 $json[$_SERVER['REMOTE_ADDR']]["blockedUntil"] = time()+$antiSpamMultiplier * $json[$_SERVER['REMOTE_ADDR']]["banCount"];
  28.             }
  29.         }else{
  30.             //New user
  31.             $json[$_SERVER['REMOTE_ADDR']]["lastConnect"] = time();
  32.             $json[$_SERVER['REMOTE_ADDR']]["spamScore"] = 0;
  33.             $json[$_SERVER['REMOTE_ADDR']]["blockedUntil"] = 0;
  34.             $json[$_SERVER['REMOTE_ADDR']]["banCount"] = 0;
  35.         }
  36.  
  37.         $f = fopen($jsonfile, "w");
  38.         fwrite($f, json_encode($json));
  39.         fclose($f);
  40.     }catch(Exception $e){
  41.         $f = fopen($jsonfile, "w");
  42.         fwrite($f, json_encode($json));
  43.         fclose($f);
  44.     }
  45.     return true;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement