Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / PHP Codes » How To Display Different Content For Different Countries Using PHP?

How To Display Different Content For Different Countries Using PHP?

As a web designer, you must experience sometime that you need to show different content for different countries in the same place to make your code more user friendly where ever the user open your web page like changing language, changing the price, changing your phone no, changing your offers, etc. So for this purpose, there are many codes available online but all of theme have some limitations. As a personal experience, we would like to recommend to you what we used.

As you know that there are many related geo-location services available over the internet that is even not free and do not offer you as a developer an easy API to retrieve the country, continent and other geodata from a server request. However, there are some products available that don’t want to pay you for the service because you are only testing, but there are many services that do the same even you are developing any commercial projects. So here we have one of the best.

An Introduction To geoPlugin

Knowing where your visitors are located in the world is essential these days when it comes to designing web sites. Whether it’s something simple as displaying banners relevant to the visitor’s country or displaying your content in the visitor’s native language, or something a little more complex such as letting your visitor know how much a product is in their own currency on that particular day, adding this type of functionality can greatly increase your site’s retention rate for visitors from all over the world.

Recommended For You:
Fully Customizable Simple Progress Circular Bar Using Pure Vanilla JavaScript

How Does It Work?

We need to make a request to the with a get parameter that needs to be the IP of the request. that will display some of the value-able available information. As you can see that we can retrieve some important information for free by hitting an example IP Address as, the following keys are available in the object :

"geoplugin_credit":"Some of the returned data includes GeoLite data created by MaxMind, available from http:\/\/<\/a>.",
"geoplugin_region":"New Jersey",
"geoplugin_regionName":"New Jersey",
"geoplugin_countryName":"United States",
"geoplugin_continentName":"North America",

How To Use It Using PHP?

To use, simply copy the below code snippet and paste it in your PHP file where you want to change the content for every country.

$geoplugin = unserialize( file_get_contents('' . $_SERVER['REMOTE_ADDR']) );
if ( is_numeric($geoplugin['geoplugin_latitude']) && is_numeric($geoplugin['geoplugin_longitude']) ) {
    $lat = $geoplugin['geoplugin_latitude'];
    $long = $geoplugin['geoplugin_longitude'];
if ($geoplugin['geoplugin_countryCode'] == 'US') {
    echo '<script type="text/javascript">
          alert("You Are In US Right Now");          
} else {
    echo '<script type="text/javascript">
          alert("You Are In Not US Right Now"); 

You can use any type of content or web codes instead of upper shown javascript as it is for example purpose only.

Last Words:

That’s all we have. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us. Don’t forget to share this with your friends so they can also take benefit from it and leave your precious feedback in our comment form below. Happy blogging, See you in the next article…

Recommended For You:
Pure Vanilla JS Infinite Responsive Slider With Navigation

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

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