fhiiqm/test/ip_intern_test.php
changeset 42 cfc409017ba8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/fhiiqm/test/ip_intern_test.php	Fri Jan 17 08:50:55 2014 +0100
@@ -0,0 +1,77 @@
+<?php
+
+/**
+ * @author Bettina Schwarzer, Fritz-Haber-Institut
+ * @copyright 01/2014
+ */
+
+function isintern($ip)
+{
+        if (!preg_match("/^141\.14\./", $ip) &&
+            !preg_match("/^172\.(1[6-9]|2[0-9]|31)\./", $ip) &&
+            !preg_match("/^10\.0\./", $ip)) {
+                echo "extern: ".$ip. "<br />";
+                return false;
+        }
+        else 
+        {
+            echo "intern: ".$ip. "<br />";
+            return false;
+        }
+}
+
+isintern($_SERVER["REMOTE_ADDR"]);
+echo ", ".ip2long($_SERVER["REMOTE_ADDR"]);
+echo "<br /><br />";
+foreach(array('171.15.255.555',
+              '172.16.0.0', 
+              '172.24.3.3', 
+              '172.16.3.3',
+              '172.18.3.3',
+              '172.24.3.3',
+              '172.31.255.255',
+              '172.32.0.0',
+              '10.0.1.128',
+              '123.123.123.123',
+              '8.8.8.8',
+              '141.14.127.255',
+              '141.14.128.0',
+              '141.14.143.255',
+              '141.14.144.0',
+              '141.14.141.141') as $ip) { 
+                isintern($ip);
+                }
+echo "<br /><br />";
+
+function islocal($ip) {
+  foreach(array('172.16.0.0/12', '141.14.128.0/20','10.0.0.0/8') as $net) {
+    list($subnet, $mask) = explode('/', $net);
+    if ((ip2long($ip) & ~((1 << (32 - $mask)) - 1) ) == ip2long($subnet)) return true;
+  }
+  return false;
+}
+
+
+
+foreach(array('171.15.255.555',
+              '172.16.0.0', 
+              '172.24.3.3', 
+              '172.16.3.3',
+              '172.18.3.3',
+              '172.24.3.3',
+              '172.31.255.255',
+              '172.32.0.0',
+              '10.0.1.128',
+              '123.123.123.123',
+              '8.8.8.8',
+              '141.14.127.255',
+              '141.14.128.0',
+              '141.14.143.255',
+              '141.14.144.0',
+              '141.14.141.141') as $ip) {
+
+  if (islocal($ip)) echo "$ip local<br>\n";
+  else echo "$ip extern<br>\n";
+}
+
+?>