Coding, PHP & MySQL

Find a visitor’s IP Address with Country and flag in PHP

This tutorial is describing how to get the IP address of the website visitor. Then this Ip will be resolved to get the country name of the visitor. Then the country name will be used to get the country flag. The files attached with this article are the countries.php, flags.php and ip_address files. Their details and importance are discussed in the related parts below.

STEP 1: Retrieve The Visitor’s IP Address

This code will retrieve the user’s IP and will be stored in a variable.

$user_ip_address=$_SERVER['REMOTE_ADDR'];

echo "User's Ip address = " . $user_ip_address;

STEP 2: Retrieve The Visitor’s Country Code

This code will use the user’s IP and will get the country code. This country code is two letter country code.

A function find_country is doing this work. Let us see how this code is working.

$no = preg_split("/./", $ip_address);

This line is splitting the Ip address into numbers based on the occurance of dot ( . ).
for example if the IP address is 100.100.100.100 then this will be divided into 4 numbers and each will be stored in the array $no.
In the above example it will be:

$no[0] = 100, $no[1]=100, $no[2]=100, $no[3]=100 


include_once("ip_files/".$no[0].".php");

We have included many files in the attached folder. Each file is unique because it contains country codes of all the IP addresses starting with a unique IP.

For instance in above example a file called ip_files/100.php will be included. This file contains country codes for the Ip address that begin with the Ip address 100.

$appropriate_code=($no[0] * 16777216) + ($no[1] * 65536) + ($no[2] * 256) + ($no[3]);

Here the IP address is converted into some appropriate code.

foreach($ranges as $key => $value){
        
            if($key<=$appropriate_code){
                        if($ranges[$key][0]>=$appropriate_code){$country_code=$ranges[$key][1];break;}
            }
    }

Data is read from file called “ip_files/100.php” to seek the range of codes which has the code we transformed($appropriate_code)

if ($country_code==""){$country_code="unkown";}
               return $country_code;
     }

If two letter country code is found then that code is returned, other wise nothing is returned.

By the above function we have got the 2 characters long country code in the variable $country_code.
$country_code=find_country($ip_address);

STEP 3: Get The country Name

include_once("ip_files/countries.php");

We have included the file countries.php. This file is attached in the zip files attached with this article. This file simple contains an array called countries which have the values like :

$countries=Array(
"AD"=> "ANDORRA",
"AE"=> "UNITED ARAB EMIRATES",
"AF"=> "AFGHANISTAN", ...........................

$three_letter_country_code=$countries[ $country_code][0];
 $country=$countries[$country_code][1];
echo "name of the visitor's country is " . $country;

By using the 2 letter country code in the above lines we will get the name of the country ($country).

STEP 4:: Get The Country Flag

$file = "flags/$country_code.gif"
if(file_exists($file)) {
      echo '<img src="flags/$file" width="29" height="14" />';
}
else {
      echo '<img src="flags/noflag.gif" width="29" height="14" />';
}

Now here we are using the 2 digit country code to get the flag file from the flags folder (This folder with main countries are attached).

The flag names are based on the 2 characters country code. For example the USA flag will be US.gif. The above code will show the flag if the flag file for that country is included in the folder.