I am looking for a function to determine whether a given IPv4 address is in a given network.
It will be similar to this; however, I do not want to install a complete framework or reinvent the wheel if it is not necessary.
The idea would be similar to the following:
function IsInNetwork($givenIP, $networkIP, $netmask) {
// ???
}
$valid = IsInNetwork("10.0.9.35", "10.0.8.0", "255.255.254.0");
— EDIT —
With Rich Adams help, he pointed me in the right direction and came up with the following:
function IsInNetwork2($givenIP, $networkIP, $netmask)
{
$ipaddr = ip2long($givenIP);
$netip = ip2long($networkIP);
$netmask = (ip2long($netmask) * -1) + $netip;
if ($ipaddr >= $netip && $ipaddr <= $netmask){
return true;
} else {
return false;
}
}
Something like this should work,