Display the country flag of visitors in PHP

By Faruque Ahamed Mollick

Today, we are going to see a very interesting topic of PHP in this post. We find on several websites that show the flag of our country. It doesn’t mean that your country flag is visible to all other visitors from other countries. All the visitors can see their own country.

That’s the magic. Each and every visitors on the web page see their own country flag. For example, if you are from the United States, then you will see the country flag of United States. But as I am from India, so I will see the flag of my country India.

Country information on clicking map – PHP script

Get visitors country and city in PHP using freegeoip API



Now we are going to create our own PHP function that will display the flag of the visitor’s country by detecting the country of the visitor. Here we are going to use the IPInfoBD API keys.

Below is our PHP function:

<?php
function visitor_country($ipinfodb_api)
{
    // Get the IP address of the visitor
    $ip_address = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ip_address = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ip_address = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ip_address = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ip_address = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
       $ip_address = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ip_address = getenv('REMOTE_ADDR');
    else
        $ip_address = 'UNKNOWN';
   
// Retrive visitor's JSON data from IP address
$ipinfodb_json = file_get_contents("http://api.ipinfodb.com/v3/ip-city/?format=json&key=".$ipinfodb_api."&ip=".$ip_address."");
// Convert the JSON data into array
$ipinfodb_arr = json_decode($ipinfodb_json);
// Get the country from the array into lower case
$country_code = strtolower($ipinfodb_arr->countryCode);

return $country_code;
}
?>

The above PHP function will detect the country and return the country code.

After that, you need flag images. I recommend you download the country flag images from GitHub. It contains the flag images in various type of files and quality. Here we are going to use the SVG quality image. After you download the zip, from GitHub, extract contents from it and take the directory “svg”. This directory contains all the flags of countries in SVG format.

How to get the complete URL query string in PHP?

How To Generate Random Password In PHP?

Now create a directory and name it flags and inside it keep the svg flag images. You can notice that all the SVG images contain the country code in lower case letter in its file name with the “.svg” extension.

As we are detecting country and return the country code from our function, so we can easily display the country flag image just by the code that you can see below:

<?php
    $country_code = visitor_country("IPInfoDB_API_KEY");
 ?>
<img width="110px" src="flags/svg/<?php echo $country_code; ?>.svg">

In the above code, you just need to put your IPInfoDB API key. After that, the code is ready to display the country flag of the visitor.

Leave a Reply

Your email address will not be published. Required fields are marked *