LATEST >>

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?

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.
geoPlugin-Banner

Recommended For You:
Smooth Direct Scroll To Anchor Link Via Mouse Or Keys

How Does It Work?

We need to make a request to the geoplugin.net/json.gp 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 http://www.geoplugin.net/json.gp?ip=162.158.63.197, the following keys are available in the object :

{
"geoplugin_request":"162.158.63.197",
"geoplugin_status":200,
"geoplugin_delay":"1ms",
"geoplugin_credit":"Some of the returned data includes GeoLite data created by MaxMind, available from http:\/\/www.maxmind.com<\/a>.",
"geoplugin_city":"Newark",
"geoplugin_region":"New Jersey",
"geoplugin_regionCode":"NJ",
"geoplugin_regionName":"New Jersey",
"geoplugin_areaCode":"",
"geoplugin_dmaCode":"501",
"geoplugin_countryCode":"US",
"geoplugin_countryName":"United States",
"geoplugin_inEU":0,
"geoplugin_euVATrate":false,
"geoplugin_continentCode":"NA",
"geoplugin_continentName":"North America",
"geoplugin_latitude":"40.7357",
"geoplugin_longitude":"-74.1724",
"geoplugin_locationAccuracyRadius":"500",
"geoplugin_timezone":"America\/New_York",
"geoplugin_currencyCode":"USD",
"geoplugin_currencySymbol":"$",
"geoplugin_currencySymbol_UTF8":"$",
"geoplugin_currencyConverter":1
}

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.

<?php
$geoplugin = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_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");          
          </script>';
} else {
    echo '<script type="text/javascript">
          alert("You Are In Not US Right Now"); 
          </script>';                                                                       
} 
}
?>

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:
How To User Get Location Details Using Pure PHP?

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 *