laie_techie
-
Posts
2 -
Joined
-
Last visited
Never
We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.
Is ipaddress in network?
in PHP Coding Help
Posted
BTW, each octet of the IP can be at most 255, so your sample IP of 192.168.1.312 was invalid.
Untested code:
[code=php:0]
// Expects $base_ip and $ip to be in dotted format
function in_network( $base_id, $bit, $ip)
{
$base_ip = ip2long( $base_ip );
$test1 = $base_ip >> ( 32 - $bits )
$ip = ip2long($ip);
$test2 = $ip >> (32 - $bits );
return $test1 == $test2;
}
if ( in_network('192.168.1.0', 22, '192.168.1.212') )
{
echo "Same network!";
}
else
{
echo "Different network!";
}
[/code]