ip-range-checker
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
function iprange($range,$mask){ // make IP range
$range = ip2long($range);
$mask = ip2long($mask);
$start = $range & $mask; // first IP
$end = $start + ~$mask; // last IP
return array('start'=>long2ip($start),'end'=>long2ip($end));
}
function in_iprange($ip,$range,$mask=null){ // check if IP is in range
if(!is_array($range)) $range = iprange($range,$mask); // create range
return (ip2long($ip) <= ip2long($range['end'])) && (ip2long($ip) >= ip2long($range['start']));
}
// voorbeeldje
$ip1 = '192.168.1.1';
$ip2 = '172.16.25.3';
$range = '192.168.0.0';
$mask = '255.255.0.0';
$iprange = iprange($range,$mask);
var_dump($iprange, in_iprange($ip1,$range,$mask), in_iprange($ip2,$iprange));
?>
function iprange($range,$mask){ // make IP range
$range = ip2long($range);
$mask = ip2long($mask);
$start = $range & $mask; // first IP
$end = $start + ~$mask; // last IP
return array('start'=>long2ip($start),'end'=>long2ip($end));
}
function in_iprange($ip,$range,$mask=null){ // check if IP is in range
if(!is_array($range)) $range = iprange($range,$mask); // create range
return (ip2long($ip) <= ip2long($range['end'])) && (ip2long($ip) >= ip2long($range['start']));
}
// voorbeeldje
$ip1 = '192.168.1.1';
$ip2 = '172.16.25.3';
$range = '192.168.0.0';
$mask = '255.255.0.0';
$iprange = iprange($range,$mask);
var_dump($iprange, in_iprange($ip1,$range,$mask), in_iprange($ip2,$iprange));
?>
Levert: