Web Hosting Talk







View Full Version : Ban IP Range with PHP


qm8309
05-06-2003, 11:55 PM
i found the following codes to ban ip addresses from executing a script.


<?php
$banned_ip = array();
$banned_ip[] = '11.111.11.x';

foreach($banned_ip as $banned) {
$ip = $_SERVER['REMOTE_ADDR'];
if($ip == $banned){
echo "You have been banned!";
exit();
}
}
// rest of PHP Script here!


i want to know, if i replace 11.111.11.x with 11.111.11.
will this script ban the entire 11.111.11 ip range? or any modification requried?

thanks.

harmonic
05-07-2003, 12:07 AM
<?php
$banned_ip = array();
$banned_ip[] = '11.111.11.x';

foreach($banned_ip as $banned) {

$ip = $_SERVER['REMOTE_ADDR'];

$ip = explode(".", $ip);
$bip = explode(".", $banned);

for($i = 0; $i < (count($bip) - 1); $i++){

if($bip[$i] == $ip[$i]){
$is_banned = TRUE;
} else {
$is_banned = FALSE;
}

}

if($is_banned === TRUE){
die("you're banned");
}
}
// rest of PHP Script here!



TRY that...

brockf
05-08-2003, 04:02 PM
Very nice, harmonic. T'is beautiful work!