note 70707 deleted from function.ip2long by cmb
| From: | cmb@php.net | Date: | Sun, 13 Oct 2019 07:46:11 +0000 |
| Subject: | note 70707 deleted from function.ip2long by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212606@lists.php.net to get a copy of this message | ||
Note Submitter: dh06 at biztechwiz dot com
----
I made one tiny change to Stephane's routine below when I had problems with spaces in an IP
range. I moved the trim function before the ip2long call.
Thanks Stephane!
Dirk.
<?php
function netMatch($network, $ip) {
$network=trim($network);
$ip = trim($ip);
$d = strpos($network,"-");
if ($d===false) {
$ip_arr = explode('/', $network);
if (!preg_match("@\d*\.\d*\.\d*\.\d*@",$ip_arr[0],$matches)){
$ip_arr[0].=".0"; // Alternate form 194.1.4/24
}
$network_long = ip2long($ip_arr[0]);
$x = ip2long($ip_arr[1]);
$mask = long2ip($x) == $ip_arr[1] ? $x : (0xffffffff << (32 - $ip_arr[1]));
$ip_long = ip2long($ip);
return ($ip_long & $mask) == ($network_long & $mask);
}
else {
$from = ip2long(trim(substr($network,0,$d)));
$to = ip2long(trim(substr($network,$d+1)));
$ip = ip2long($ip);
return ($ip>=$from and $ip<=$to);
}
}
?>