Saturday, March 12, 2011

A PHP Script To Trace Country From IP Address

Basic script for country identification of IP

In order to use this script, download compressed Database files in the top of this page, save them within a directory named "ip_files", and use the function bellow to get a two letters country code. If you want to identify visitors from specific countries, check here
$value){
if($key<=$code){ if($ranges[$key][0]>=$code){$country=$ranges[$key][1];break;}
}
}
if ($country==""){$country="unkown";}
return $country;
}

$two_letter_country_code=iptocountry("101.102.103.104");

function iptocountry($ip) {
$numbers = preg_split( "/\./", $ip);
include("ip_files/".$numbers[0].".php");
$code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
foreach($ranges as $key => $value){
if($key<=$code){
if($ranges[$key][0]>=$code){$country=$ranges[$key][1];break;}
}
}
if ($country==""){$country="unkown";}
return $country;
}
?>

How the function works:
• Line 5: IP address will be split into individual numbers and saved to an array($numbers).
• Line 6: Based in the first number of the IP address ("101" in the example), a PHP file in ip_files/ directory will be included (in the example the file to be included will be "ip_files/101.php"). This file has known country codes for IP addresses starting with the selected first number (p.e: 101.###.###.###, where # is any digit).
• Line 7: IP address is transform into appropriate code.
• Line 8-12: Data from "ip_files/101.php" is checked in order to find a range of codes which includes the code obtained by transforming our IP.
• Line 13: In case IP address is not included in the database, the value for $country will be "unkown".
• Line 14: Two letter country code is returned. In case no matches are obtained, the function will return "" (nothing).
Hope it works fine for you.


Getting the IP address of visitors and displaying the country
In case we want to identify geographical location of visitors, we must get from them the IP address.
The IP address of visitors will be contained in the enviromental variable $REMOTE_ADDR
In the example bellow, depending upon country code, info 1 or info 2 is shown:
$value){
if($key<=$code){ if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;}
}
}
if ($two_letter_country_code==""){$two_letter_country_code="unkown";}
return $two_letter_country_code;
}
?>

Getting the IP address of visitors and displaying three letters country code or complete country name
On request, we have added to the compressed document a file named countries.php.
This file may be used to display three letters country code or complete name of country. The code bellow allows getting both data:
";
print "Three letters code: $three_letter_country_code
";
print "Country name: $country_name
";

function iptocountry($ip) {
$numbers = preg_split( "/\./", $ip);
include("ip_files/".$numbers[0].".php");
$code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
foreach($ranges as $key => $value){
if($key<=$code){ if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;}
}
}
if ($two_letter_country_code==""){$two_letter_country_code="unkown";}
return $two_letter_country_code;
}
?>


Getting the IP address of visitors, and displaying country and flag

In case you want to display flags in your page, download Flags file in the top of this page, and save all pictures to a folder named "flags" .

We have added a few lines of code to script is previous example (in red) which allows showing country specific flags in our pages. This code will check whether a gif file containing the two country code exists in "flags" folder and displays it. In case the gif file in not in the folder, a default white flag is displayed. In case you have any of the missing flags, please send them to us.
";
print "Three letters code: $three_letter_country_code
";
print "Country name: $country_name
";

// To display flag
$file_to_check="flags/$two_letter_country_code.gif";
if (file_exists($file_to_check)){
print "
";
}else{
print "
";
}

function iptocountry($ip) {
$numbers = preg_split( "/\./", $ip);
include("ip_files/".$numbers[0].".php");
$code=($numbers[0] * 16777216) + ($numbers[1] * 65536) + ($numbers[2] * 256) + ($numbers[3]);
foreach($ranges as $key => $value){
if($key<=$code){ if($ranges[$key][0]>=$code){$two_letter_country_code=$ranges[$key][1];break;}
}
}
if ($two_letter_country_code==""){$two_letter_country_code="unkown";}
return $two_letter_country_code;
}
?>

No comments:

Post a Comment

PHP | PHP Books | PHP Tutorials | PHP Development