Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function checkAntispam(){
- $antiSpamTime = 2; //How fast does to connections have to be to be considered spam?
- $antiSpamMaxScore = 1; //Number of times you can spam untill getting temporaly banned
- $antiSpamMultiplier = 5; //Set a multiplier to figure out ban time. Ban time is calculated like this: bantime=multiplier*number of bans
- $jsonfile = "./antispam.json"; //Data file for storing
- $json = NULL;
- try{
- $json = file_get_contents($jsonfile);
- $json = json_decode($json, true);
- if(isset($json[$_SERVER['REMOTE_ADDR']])){
- //We have seen the user before
- if($json[$_SERVER['REMOTE_ADDR']]["blockedUntil"] > time()){ // User is banned
- return false;
- }
- if(time()-$json[$_SERVER['REMOTE_ADDR']]["lastConnect"] < $antiSpamTime){ //If user has connected within x seconds again, then increment spamscore
- $json[$_SERVER['REMOTE_ADDR']]["spamScore"] += 1;
- $json[$_SERVER['REMOTE_ADDR']]["lastConnect"] = time();
- }else{ //Hvis ikke, så ingen spamscore.
- $json[$_SERVER['REMOTE_ADDR']]["lastConnect"] = time();
- $json[$_SERVER['REMOTE_ADDR']]["spamScore"] = 0;
- if($json[$_SERVER['REMOTE_ADDR']]["banCount"] > $antiSpamMaxScore){
- $json[$_SERVER['REMOTE_ADDR']]["banCount"] = $antiSpamMaxScore; //Max score!
- }
- }
- if($json[$_SERVER['REMOTE_ADDR']]["spamScore"] >= $antiSpamMaxScore){ //Brugerens spamscore er for høj
- $json[$_SERVER['REMOTE_ADDR']]["banCount"] = $json[$_SERVER['REMOTE_ADDR']]["banCount"] + 1;
- $json[$_SERVER['REMOTE_ADDR']]["blockedUntil"] = time()+$antiSpamMultiplier * $json[$_SERVER['REMOTE_ADDR']]["banCount"];
- }
- }else{
- //New user
- $json[$_SERVER['REMOTE_ADDR']]["lastConnect"] = time();
- $json[$_SERVER['REMOTE_ADDR']]["spamScore"] = 0;
- $json[$_SERVER['REMOTE_ADDR']]["blockedUntil"] = 0;
- $json[$_SERVER['REMOTE_ADDR']]["banCount"] = 0;
- }
- $f = fopen($jsonfile, "w");
- fwrite($f, json_encode($json));
- fclose($f);
- }catch(Exception $e){
- $f = fopen($jsonfile, "w");
- fwrite($f, json_encode($json));
- fclose($f);
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement