note 41576 modified in function.ip2long by danbrown
| From: | danbrown@php.net | Date: | Fri, 05 Dec 2008 13:43:50 +0000 |
| Subject: | note 41576 modified in function.ip2long by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147700@lists.php.net to get a copy of this message | ||
Mix of rbsmith's function and pasted above url:
<?php
// returns 1 if $ip is part of $network
function IP_Match($network, $ip) {
$ip_arr = explode("/",$network);
$network_long=ip2long($ip_arr[0]);
$mask_long= pow(2,32)-pow(2,(32-$ip_arr[1]));
$ip_long=ip2long($ip);
if (($ip_long & $mask_long) == $network_long) {
return 1;
} else {
return 0;
}
}
// usage
$network="200.100.50.0/23";
$ip="200.100.51.55";
$ip2="200.100.52.2";
echo IP_Match($network, $ip); //prints 1
echo IP_Match($network, $ip2); //prints 0
?>
--was--
Mix of rbsmith's function and pasted above url:
<?
// returns 1 if $ip is part of $network
function IP_Match($network, $ip) {
$ip_arr = explode("/",$network);
$network_long=ip2long($ip_arr[0]);
$mask_long= pow(2,32)-pow(2,(32-$ip_arr[1]));
$ip_long=ip2long($ip);
if (($ip_long & $mask_long) == $network_long) {
return 1;
} else {
return 0;
}
}
// usage
$network="200.100.50.0/23";
$ip="200.100.51.55";
$ip2="200.100.52.2";
echo IP_Match($network, $ip); //prints 1
echo IP_Match($network, $ip2); //prints 0
?>
http://php.net/manual/en/function.ip2long.php