<?php
/* Listado con las IPs */
$ban_ip_list = array('68.180.206.184', '64.233.167.99', '207.46.232.182');
/* Listado con el rango de IP. Use the '*' as the range selector */
$ban_ip_range = array('69.*.83.197');
/* Ip Visitante */
$user_ip = $_SERVER['REMOTE_ADDR'];
/* Mensaje que verá si está baneado */
$msg = 'You do not have permission to access this page.';
/* Message to output if the IP is in the ban list */
if(in_array($user_ip, $ban_ip_list))
{
exit($msg);
}
/* Check if the Visitor's IP is in our range's list */
if(!empty($ban_ip_range))
{
foreach($ban_ip_range as $range)
{
$range = str_replace('*','(.*)', $range);
if(preg_match('/'.$range.'/', $user_ip))
{
exit($msg);
}
}
}
?>
Explicación de cómo banear correctamente un rango:
Ayuda:Cómo bloquear un rango
http://es.wikipedia.org/wiki/Ayuda:C%C3%B3mo_bloquear_un_rango
Usando mejor siempre el sufijo /24 para bloquear 256 ip's
<?
$banned[0]="x.x.x.x";
if (in_array($_SERVER['REMOTE_ADDR'],$banned))
{
header("HTTP/1.1 403 Forbidden");
exit;
}
?>
<?php
$ipArray = file('ip.txt');
foreach ($ipArray as $ipTest) {
if (substr_count($_SERVER['REMOTE_ADDR'],trim($ipTest)) != "0") {
header('location: /banned.htm'); // the banned display page
die();
}
}
?>
<?
$targetAddr = "123.123..*..*"; //yes is two dots
//this code will match any class of 123.123.x.x,
//you can also do "123.123.123..*" to do anything that is 123.123.123.x
if (ereg($targetAddr, $_SERVER['REMOTE_ADDR'])) {
//remote address match, do something or don't do anything
} else {
//do whatever you want to address that doesn't match
}
?>
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$ips_baneadas = array('10.0.0.1','192.0.0.1');
$contador = count($ips_baneadas);
for ($i=0; $i<$contador; $i++) {
if($ip == $ips_baneadas[$i]) { die("Tu Ip no esta permitida . $ip" ; } }
?>
Mirando un listado de ip's un fichero txt
<?php
$count_blocks = 0;
$ip_lines = file("includes/ban.txt");
$count = count($ip_lines);
foreach($ip_lines as $single_line){
$ip = explode("#", $single_line);
if($ip[1] == $_SERVER['REMOTE_ADDR']){
die("Ip baneada.");
}
}
?>
<?php
$banned_ip = array();
$banned_ip[] = '111.111.111.111';
$banned_ip[] = '111.111.111.112';
$banned_ip[] = '111.111.111.113';
$banned_ip[] = '111.111.111.114';
foreach($banned_ip as $banned) {
$ip = $_SERVER['REMOTE_ADDR'];
if($ip == $banned){
echo "You have been banned!";
exit();
}
}
// rest of PHP Script here!
?>
<?php
function ban($range = array(), $ip = '')
{
$ip = longip(trim($ip));
if($ip == FALSE)
{
return FALSE;
}
if(empty($ip))
{
return FALSE;
}
foreach($range AS $key => $val)
{
$temp = explode('-', $val);
if(empty($temp[0]))
{
return FALSE;
}
else
{
$start_ip = longip(trim($temp[0]));
if($start_ip == FALSE)
{
return FALSE;
}
}
if(empty($temp[1]))
{
if($ip == $start_ip)
{
return TRUE;
}
}
else
{
$stop_ip = longip(trim($temp[1]));
if($stop_ip == FALSE)
{
return FALSE;
}
}
if($start_ip <= $ip && $ip <= $stop_ip)
{
return TRUE;
}
}
return FALSE;
}
function longip($ip)
{
if(empty($ip))
{
return FALSE;
}
$block = explode('.', $ip);
if(count($block) != 4)
{
return FALSE;
}
$i = 3;
$block_ip = 0;
foreach($block as $k => $v)
{
$v = filter_var($v, FILTER_VALIDATE_INT);
if($v < 0)
{
$v = 0;
}
if($v > 255)
{
$v = 255;
}
$block_ip += pow(256, $i)*$v;
$i--;
}
return $block_ip;
}
//usage
$block_range = array( '192.168.1.0 - 192.168.2.255',
'192.168.4.0 - 192.168.4.255',
'192.168.6.1'
);
$ip = '192.168.3.255';
if(ban($block_range, '192.168.6.1'))
{
echo "BAN\n";
}
else
{
echo "UNBAN\n";
}
?>