Country identification based on IP address


Introduction: country identification without databases
Basic script for country identification of IP
Getting the IP address of visitors and displaying the country
Getting the IP address of visitors and displaying three letters code or complete country name
Getting the IP address of visitors,  and displaying country and flag

Download files
Complete database: .zip (787 K; update: 2019/02/08)
Flags (.zip, 130 K)


Introduction: country identification without databases

This script was originally developed for geolocalization of visitors to my site. I had not database access, so in order to make the script fast, I splitted the information from IP to Country Database (provided by software77.net) into several smaller files. All those files may be download. Check this sample file.

Bellow you will find examples of code allowing to display the country.

Check here for response to some questions from users

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.

<?php
$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;
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

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:

<?php
$IPaddress=$_SERVER['REMOTE_ADDR'];
$two_letter_country_code=iptocountry($IPaddress);

if ($two_letter_country_code=="US"){
    print "This is ad number 1, because you are from USA";
    }else{
    print "This is ad number 2, because you are not from USA";
    }

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 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:
<?php
$IPaddress=$_SERVER['REMOTE_ADDR'];
$two_letter_country_code=iptocountry($IPaddress);
 
include("IP_FILES/countries.php");
$three_letter_country_code=$countries[ $two_letter_country_code][0];
$country_name=$countries[$two_letter_country_code][1];

print "Two letters code: $two_letter_country_code<br>";
print "Three letters code: $three_letter_country_code<br>";
print "Country name: $country_name<br>";

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.

<?php
$IPaddress=$_SERVER['REMOTE_ADDR'];
$two_letter_country_code=iptocountry($IPaddress);
 
include("IP_FILES/countries.php");
$three_letter_country_code=$countries[ $two_letter_country_code][0];
$country_name=$countries[$two_letter_country_code][1];

print "Two letters code: $two_letter_country_code<br>";
print "Three letters code: $three_letter_country_code<br>";
print "Country name: $country_name<br>";

// To display flag
$file_to_check="flags/$two_letter_country_code.gif";
if (file_exists($file_to_check)){
                print "<img src=$file_to_check width=30 height=15><br>";
                }else{
                print "<img src=flags/noflag.gif width=30 height=15><br>";
                }

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;
}
?>


2016@ PhpTutorial.info. All rights reserved. Contact