How to automatically detect the country of a user?

Using the free API service provided by, you can add code into hooks for detecting the country of the current user, given his IP address. Here is how this code looks like:

  $country = 'Unknown'; // default in case country can't be detected
  $ip = $_SERVER['REMOTE_ADDR'];
  $udata_json = @file_get_contents("{$ip}");
    $udata = json_decode($udata_json, true);
    $country = $udata['country'];
  // you can now use $country in your code ...

Here is a demonstration that shows your country!

Your country is Unknown

Country info retrieved from this service is ISO Alpha-2 abbreviation.