summaryrefslogtreecommitdiff
path: root/ipmatch.php
diff options
context:
space:
mode:
Diffstat (limited to 'ipmatch.php')
-rw-r--r--ipmatch.php17
1 files changed, 17 insertions, 0 deletions
diff --git a/ipmatch.php b/ipmatch.php
new file mode 100644
index 0000000..058f878
--- /dev/null
+++ b/ipmatch.php
@@ -0,0 +1,17 @@
+<?php
+function match_network ($nets, $ip) {
+ foreach ($nets as $net) {
+ $ip_arr = explode('/', $net);
+ if (!$ip_arr[1])
+ $ip_arr[1] = '32';
+
+ $net_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);
+
+ if (($ip_long & $mask) == ($net_long & $mask)) return true;
+ }
+ return false;
+}
+?>