Cjwinnit
10-03-2011, 05:59 PM
As my first post I thought it might be a laff for you guys to look at one of my scripts.
Ever wanted a to see if an IP is within a list of ip's? is 145.78.4.3 in either 145.7.*.* or bla.bla.bla.* ?
First you need the folowing two functions:
####function used in checking for subnets#############
function ipto32bin ( $ipthingee )
{
$ipthingee = explode(".",$ipthingee);
$iinf = 0; do {
$ipthingee[$iinf] = decbin($ipthingee[$iinf]);
$iinf++;} while($iinf<=3);
$ipasabin = sprintf("%08d%08d%08d%08d", $ipthingee[0],$ipthingee[1],$ipthingee[2],$ipthingee[3]);
return $ipasabin;
}
###########################
###########################
function isipinsub ( $ip, $ipsub )
{
$x = strpos($ipsub, "/");
$lengthcomp = substr($ipsub, $x + 1);
$subip = substr($ipsub, 0, $x);
$ip = ipto32bin($ip);
$subip = ipto32bin($subip);
$ip = substr($ip,0,$lengthcomp);
$subip = substr($subip,0,$lengthcomp);
if($ip === $subip) return TRUE;
else return FALSE;
}
########################################################
You now have a function that will return true or false as a boolean. Examples:
isipinsub ( 192.168.0.50, 192.168.0.0/24 ) will return TRUE,
isipinsub ( 73.56.4.5, 10.0.0.0/8 ) will return FALSE.
You can use this if you have a list of collections of IPs you wish to ban, or take your page offline to everyone except a particular subnet.
How I use it:
###maintenance page#########
if($ini_page[kill_page] == "true"){
if($ini_page[kill_override] == "false")
{ require ("pages/maintenance.php"); die; }
if(!isipinsub($_SERVER["REMOTE_ADDR"], $ini_page[kill_override_iprange]) &&
!in_array($_SERVER["REMOTE_ADDR"],$ini_page[kill_override_ip_list]))
{ require ("pages/maintenance.php"); die; }
}
############################
#######checks ban list######
if(($ini_page[kill_page] != "true") && ($ini_page[banned_ipranges][0] != "")){
foreach($ini_page[banned_ipranges] as $val)
{
if(isipinsub ($_SERVER["REMOTE_ADDR"], $val)){ require ("pages/maintenance.php"); die; }
}}
###end maintenance page####
Enjoy!
Ever wanted a to see if an IP is within a list of ip's? is 145.78.4.3 in either 145.7.*.* or bla.bla.bla.* ?
First you need the folowing two functions:
####function used in checking for subnets#############
function ipto32bin ( $ipthingee )
{
$ipthingee = explode(".",$ipthingee);
$iinf = 0; do {
$ipthingee[$iinf] = decbin($ipthingee[$iinf]);
$iinf++;} while($iinf<=3);
$ipasabin = sprintf("%08d%08d%08d%08d", $ipthingee[0],$ipthingee[1],$ipthingee[2],$ipthingee[3]);
return $ipasabin;
}
###########################
###########################
function isipinsub ( $ip, $ipsub )
{
$x = strpos($ipsub, "/");
$lengthcomp = substr($ipsub, $x + 1);
$subip = substr($ipsub, 0, $x);
$ip = ipto32bin($ip);
$subip = ipto32bin($subip);
$ip = substr($ip,0,$lengthcomp);
$subip = substr($subip,0,$lengthcomp);
if($ip === $subip) return TRUE;
else return FALSE;
}
########################################################
You now have a function that will return true or false as a boolean. Examples:
isipinsub ( 192.168.0.50, 192.168.0.0/24 ) will return TRUE,
isipinsub ( 73.56.4.5, 10.0.0.0/8 ) will return FALSE.
You can use this if you have a list of collections of IPs you wish to ban, or take your page offline to everyone except a particular subnet.
How I use it:
###maintenance page#########
if($ini_page[kill_page] == "true"){
if($ini_page[kill_override] == "false")
{ require ("pages/maintenance.php"); die; }
if(!isipinsub($_SERVER["REMOTE_ADDR"], $ini_page[kill_override_iprange]) &&
!in_array($_SERVER["REMOTE_ADDR"],$ini_page[kill_override_ip_list]))
{ require ("pages/maintenance.php"); die; }
}
############################
#######checks ban list######
if(($ini_page[kill_page] != "true") && ($ini_page[banned_ipranges][0] != "")){
foreach($ini_page[banned_ipranges] as $val)
{
if(isipinsub ($_SERVER["REMOTE_ADDR"], $val)){ require ("pages/maintenance.php"); die; }
}}
###end maintenance page####
Enjoy!