PHP: detecting IP's entry to a specific IP range

Posted by ilnur777 on Stack Overflow See other posts from Stack Overflow or by ilnur777
Published on 2010-05-03T23:31:23Z Indexed on 2010/05/03 23:38 UTC
Read the original article Hit count: 245

Filed under:
|
|
|

I have the PHP function that determines whether one IP goes to a specific IP range, but I don't know how to find out the IP's network and mask. Can anyone help with this?

<?
// Example of calling and checking IP-address 192.168.0.4
// belonging to a network 192.168.0.0 with mask 255.255.255.248

if(ip_vs_net("192.168.0.4","192.168.0.0","255.255.255.248")){
print "Address belongs to a netwok<BR>";
} else {
print "Address is out of subnetwork's range<BR>";
}

function ip_vs_net($ip,$network,$mask){
if(((ip2long($ip))&(ip2long($mask)))==ip2long($network)){
return 1;
} else {
return 0;
}
}
?>

© Stack Overflow or respective owner

Related posts about php

Related posts about ip