How to Implement Country Based Redirection Using PHP

Ip to country with PHP

If you are working on a website that has different content for different countries, you must be thinking of implementing country based redirection. There could be lots of ways of doing this. You can use .htaccess method by using the mod_geoip module. But what if you want to do this with PHP. In this case, you need the IP address to the country database. If you have this, you can easily capture the IP address of the visitor, you can get the location and perform redirection.

But getting the IP address to the country database is also not an easy task. So, Here I have a better and easier way.

The website freegeoip.net offers a free to use API and allows 15000 queries per hour by default. That is more than enough for a website with up to 3,00,000 views a day.

There is no need to register for using this api. If you have the IP address of the visitor, you just need to pass in the API URL to get the country. Here is the code.

/// get IP address of visitor

if(! empty($_SERVER['REMOTE_ADDR']) ){
    $ip = $_SERVER['REMOTE_ADDR'];
}
else{
    $ip = empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? '' : $_SERVER['HTTP_X_FORWARDED_FOR'];
}

/// Get the country

$ipcon="http://freegeoip.net/json/".$ip;
$ipcon = file_get_contents($ipcon);
$ipArray = json_decode($ipcon, true);
$country = strtolower($ipArray['country_code']);

// Here I am using US as base country and countr.domain.com for other countries.
if($country!='us')
{
$loc='http://'.$country.'.domain.com;
header("Location: $loc");
}

The API returns Time/zone, postal code, city, country, country_code, and region_name. So, you have enough data to use on your website. Now you have the country of the user. I used country code as I wanted to use it for redirection.

There are few other similar websites, but they never confirm the API usage limit. So, this solution is not just simple but also has a good allowable rate that can even be used on a high traffic website.

Alternatively, you can also try geoplugin.com. They also have a similar easy to use API.

// use previous code for capturing IP addrss.

$ipcon="http://www.geoplugin.net/php.gp?ip=".$ip;
$ipcon = file_get_contents($ipcon);
$ipArray = json_decode($ipcon, true);
$country = strtolower($ipArray['geoplugin_countryCode']);

// now use the previous code of redirection

Hostip.info also got something similar to try. But they offer you text results in place of JSON or XML data. So, you need to parse the text. Here is how to get a result.

$result = "http://api.hostip.info/get_html.php?ip=".$ip

I tested Hostip.info for several IP address and couldn’t find it worth to use. Most of the results were wrong. So, go with the solution I mentioned at first.

Final Words

I tried to give good options for getting the country from IP and then do redirection based on that. I personally use freegeoip.net on a few of my projects and recommend the same. If you have any questions to ask, you can always leave it in the comment.

Advertisement

Tags: |

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip | Part of Techlomedia Internet Pvt Ltd Developed By Deepanker