note 54953 deleted from function.ip2long by cmb
| From: | cmb@php.net | Date: | Sun, 18 Aug 2019 08:45:19 +0000 |
| Subject: | note 54953 deleted from function.ip2long by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211945@lists.php.net to get a copy of this message | ||
Note Submitter: cam at wecreate dot com
----
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
?>