Dragana
Dragana

Reputation: 33

Bann one or more IP address with PHP withouthtaccess using .htaccess

I'm looking for good code in PHP for Banning some spammers IP's My server is giving me error 500 if I'm using .htaccess

Upvotes: 1

Views: 60

Answers (2)

Evadecaptcha
Evadecaptcha

Reputation: 1451

The simplest way would be to have a database that keeps a list of the banned ip addresses, if you want to do it on the PHP end rather than directly in the server.

for($i = 0;$i < count($listOfIps);$i++) {
    if($listOfIps[$i] == filteredIP($_SERVER['REMOTED_ADDR'])) { //filteredIP is not a native function, it's just a representation of however you want to filter the ip addresses which are sent to you
        $banned = true;
    }
}

if($banned):
    //redirect user or kill script
else:
    //render page

endif;

However, there may be better solutions based on the page or application specifics, but this is the best solution I can think of based on your question

Upvotes: 0

Ненад
Ненад

Reputation: 341

This will do the work

$getip = $_SERVER["REMOTE_ADDR"]; 
$banned_ip = array(); 

$banned_ip[] = '194.9.94.*'; 
$banned_ip[] = '77.105.2.*'; 

foreach($banned_ip as $banned) 
{ 
$blacked=str_replace('*', '', $banned); 
$len=strlen($blacked); 
if ($getip==$blacked || substr($getip, 0, $len)==$blacked) 
{ 
$_banned_ip=true; 
} 
} 

if($_banned_ip==true){  
echo 'THIS IP IS BANNED!'; 
exit;  
}  

Upvotes: 1

Related Questions