Country identification based on IP address: identification of specific countries

Introduction

In case we want to identify visitors only from US (or a different country), downloading all the information in our database may be a waste of time and resources. That is why we have created a set of databases for specific country identification.

We have faced this problem in two ways:


For UNITED STATES, UNITED KINGDOM, GERMANY, FRANCE and NETHERLANDS, download the databases in the table.

Use this databases instead of the general database with all countries when you want to identify visitors from these countries. Decompress the file and save all content within a directory named "ip_files".
Country Database (2012/01/15)
UNITED STATES download (253 Kb)
UNITED STATES + CANADA
download (286 Kb)
UNITED KINGDOM download (59 Kb)
GERMANY download (68 Kb)
FRANCE download (45 Kb)
NETHERLANDS download (44 Kb)

There are a lot of IP ranges for each of those countries. In case a unique file is generated for countries in this list, the size of the file will be over 100 Kb. Requesting this long file with lots of IP ranges and searching it each time a visitor gets to our site will not be very efficient. That is why we are providing these spleeted databases.
    The following script may be used as a reference to identify visitors from United States and to respond to them with specific content:

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

    if ($two_letter_country_code=="US"){
         include("This_file_for_US_visitors.html");
          die();
        }else{
         include("This_file_for_other_countries.html");
          die();
        }

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


For countries not included in the list above, select country in the form and get the file with IP ranges.


Select country

When identifying countries in the list, download corresponding database. After selection of the country, copy the content in the textarea to a file named "ip_database.php".

For countries in the list, number of IP ranges is not very big. That is why we are including all IP ranges in an unique file. Country identification will be efficient enought for all countries.

The following script may be used as a reference to identify visitors from Spain (ES) and to respond to them with specific content:

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

if ($two_letter_country_code=="ES"){
     include("This_file_for_ES_visitors.html");
      die();
    }else{
     include("This_file_for_other_countries.html");
      die();
    }

function iptocountry($ip) {   
    $numbers = preg_split( "/\./", $ip);   
    include("ip_database.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;
}
?>

 



2010@ PhpTutorial.info. All rights reserved. Contact