3/17/2016 - 5:00 PM

Detect Country Code using PHP freegeoip API

Detect Country Code using PHP freegeoip API provides a public HTTP API for software developers to search the geolocation of IP addresses. It uses a database of IP addresses that are associated to cities along with other relevant information like time zone, latitude and longitude.

You're allowed up to 10,000 queries per hour by default. Once this limit is reached, all of your requests will result in HTTP 403, forbidden, until your quota is cleared.

The freegeoip web server is free and open source so if the public service limit is a problem for you, download it and run your own instance.
// Steve Berrill
// GitHub
$geoip = file_get_contents('' . $_SERVER['REMOTE_ADDR']);
$parsedgeoip  = json_decode($geoip);
$countryCode = $parsedgeoip->country_code;

if ($countryCode !== 'GB') {echo '+44';} // echo UK country dial code if not GB(UK)