Prova questo (testato ma non troppo).
Formatazzione degli ip :
ip fisso : 99.98.97.96 banna questo ip.
range ip : 99.98.97.90-150 banna gli ip da 99.98.97.90 a 99.98.97.150
puoi applicare un range su qualsiasi parte di un ip.
Esempio : Se vuoi bannare tutti gli ip che comiciano da 10 => 10.0-255.0-255.0-255
Codice PHP:
<?php
//============================================================================
function IPBannata($indirizzoIP,$listaIPBannati) {
$partIP = '([0-9]{1,3}|[0-9]{1,3}[-]{0,1}[0-9]{1,3})';
$pattern = "/\s*{$partIP}[.]{$partIP}[.]{$partIP}[.]{$partIP}\s*(?:\r\n|$)/m";
preg_match_all($pattern,$listaIPBannati,$arrIPBannate);
$pattern = "/^{$partIP}[.]{$partIP}[.]{$partIP}[.]{$partIP}$/";
$indirizzoIP = trim($indirizzoIP);
preg_match_all($pattern,$indirizzoIP,$arrIPDaTestare);
$ipDT01 = $arrIPDaTestare[1][0];
$ipDT02 = $arrIPDaTestare[2][0];
$ipDT03 = $arrIPDaTestare[3][0];
$ipDT04 = $arrIPDaTestare[4][0];
$conta = count($arrIPBannate[0]);
for($i=0;$i<$conta;$i++) {
$ipBA01 = explode('-',$arrIPBannate[1][$i]);
$ipBA02 = explode('-',$arrIPBannate[2][$i]);
$ipBA03 = explode('-',$arrIPBannate[3][$i]);
$ipBA04 = explode('-',$arrIPBannate[4][$i]);
$countOK = 0;
$countOK = checkParteIP($ipDT01,$ipBA01) ? $countOK + 1 : $countOK;
$countOK = checkParteIP($ipDT02,$ipBA02) ? $countOK + 1 : $countOK;
$countOK = checkParteIP($ipDT03,$ipBA03) ? $countOK + 1 : $countOK;
$countOK = checkParteIP($ipDT04,$ipBA04) ? $countOK + 1 : $countOK;
if ($countOK == 4) {
return true;
}
} // for($i=0;$i<$conta;$i++)
return false;
} // function IPBannata($indirizzoIP,$listaIPBannati)
function checkParteIP($parteIP,$arrayRangeIP) {
if (count($arrayRangeIP) == 1) {
if ($parteIP == $arrayRangeIP[0]) {
return true;
} else {
return false;
} // if ($parteIP == $arrayRangeIP[0])
} else {
if (($parteIP >= $arrayRangeIP[0]) && ($parteIP <= $arrayRangeIP[1])) {
return true;
} else {
return false;
} // if ($parteIP == $arrayRangeIP[0])
} // if (count($arrayRangeIP) == 1)
} // function checkParteIP($parteIP,$arrayRangeIP)
//============================================================================
$listaIPBannati = file_get_contents('lista_ip.txt');
print "Lista degli IP bannati:<br/>";
print nl2br($listaIPBannati);
print "<hr/>";
$ipList = array('127.128.129.130',
'10.50.100.0',
'10.50.120.255',
'10.50.121.255',
'11.30.111.19',
'11.30.111.20',
'11.30.111.21',
'11.30.111.22',
'11.30.111.23',
'11.30.111.24',
'1.2.3.4',
'1.2.3.5',
'250.251.252.253');
foreach($ipList as $ip) {
print "IP $ip :";
$result = IPBannata($ip,$listaIPBannati);
$testo = $result ? "bannato" : "non bannato";
print "$testo<br/>";
} // foreach($ipList as $ip)
?>
File lista_ip.txt
codice:
10-20.50-60.100-120.0-255
11.30.111.20-23
24.123.12-16.1
99.98.97.96
1.2.3.4
250.251.252.253