Shares

What if I could show you a way to restrict users on your website by geo-location? With just a few lines of PHP code, you can restrict, or depending on the situation, show, different content to users from different countries. I am sure you get the idea.

//get the country code
$country = file_get_contents("http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}/country");

//trim any whitespace
$c = trim($country);

//if country is Italy
if( ($c == "IT") ) {
echo "ciao";

//else if country is Britain
} else if ( ($c == "GB") ) {
echo "hello";

//else restrict access
} else {
echo "access restricted";
}

Country Code List

Here’s a list of ISO 3166-1 country codes.

About Ali Gajani

Hi. I am Ali Gajani. I started Mr. Geek in early 2012 as a result of my growing enthusiasm and passion for technology. I love sharing my knowledge and helping out the community by creating useful, engaging and compelling content. If you want to write for Mr. Geek, just PM me on my Facebook profile.