IP2Country Script
Kurze Erläuterung zu dem IP2Country Script von webmaster-eye.de.
Mit dem auch auf webmaster-eye.de befindlichen
IP2Country Tool, können Sie IP Adressen nach Ländern auflösen.
Das heißt Sie geben eine IP Adresse ein und erhalten als Ergebnis das jeweilige Land aus dem die IP Adresse stammt.
Als Datenbank verwende ich die CSV Datei
ip-to-country.csv von
ip-to-country.webhosting.info.
Das PHP Script was dann die IP Adresse auswertet sieht wie folgt aus:
- <?php
- function findcountry($ip){
-
- $countrydb = file("/pfad/zu/ip-to-country.csv");
-
-
- $ip_number = sprintf("%u",ip2long($ip));
-
-
- $low = 0;
- $high = count($countrydb) -1;
- $count = 0;
-
- while($low <= $high) {
-
- $count++;
- $mid = floor(($low + $high) / 2);
- $num1 = substr($countrydb[$mid], 1, 10);
- $num2 = substr($countrydb[$mid], 14, 10);
-
- if($num1 <= $ip_number && $ip_number <= $num2){
-
-
- print "Länderkennung: " . substr($countrydb[$mid], 27, 2);
- break;
-
- } else {
-
- if ($ip_number < $num1) {
-
- $high = $mid - 1;
-
- } else {
-
- $low = $mid + 1;
-
- }
-
- }
-
- }
-
- }
- ?>
Durch den Aufrufen der Funktion
- <?php
- findcountry("123.456.789.000");
- ?>
erhält man dann den entsprechenden Ländercode.