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;
}
}
?>
When the IP address was classy (Class A, B, C etc), it was easy to find the subnet mask because it’s fixed depending on the address ranges.
Now with CIDR, it’s impossible to know the exact subnet mask because any contiguous prefix can be used as subnet mask.
However, the classy subnet might work for your case. It’s definitely better than nothing. You can figure out the subnet mask using this function,