note 54953 modified in function.ip2long by danbrown
| From: | danbrown@php.net | Date: | Fri, 05 Dec 2008 13:41:47 +0000 |
| Subject: | note 54953 modified in function.ip2long by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147696@lists.php.net to get a copy of this message | ||
A simple function to compare two IP addresses against a netmask. Useful if you're locking down
a web app with an IP address, but can't force the IPs to be exactly the same.
<?php
function ipcompare ($ip1, $ip2, $mask) {
$masked1 = ip2long($ip1) & ip2long($mask); // bitwise AND of $ip1 with the mask
$masked2 = ip2long($ip2) & ip2long($mask); // bitwise AND of $ip2 with the mask
if ($masked1 == $masked2) return true;
else return false;
}
?>
Examples:
<?php
ipcompare("192.168.1.63","192.168.1.65","255.255.255.0") // true
ipcompare("192.168.1.63","192.168.1.65","255.255.255.192") // false
?>
--was--
A simple function to compare two IP addresses against a netmask. Useful if you're locking down
a web app with an IP address, but can't force the IPs to be exactly the same.
function ipcompare ($ip1, $ip2, $mask) {
$masked1 = ip2long($ip1) & ip2long($mask); // bitwise AND of $ip1 with the mask
$masked2 = ip2long($ip2) & ip2long($mask); // bitwise AND of $ip2 with the mask
if ($masked1 == $masked2) return true;
else return false;
}
Examples:
ipcompare("192.168.1.63","192.168.1.65","255.255.255.0") // true
ipcompare("192.168.1.63","192.168.1.65","255.255.255.192") // false
http://php.net/manual/en/function.ip2long.php