Learn how to detect the country of a request with php or javascript easily and without pay a cent.

How to detect the country of a visitor in php or javascript for free with the request ip

There are many services that are not free and offer you as a developer an easy API to retrieve the country, continent and other geodata from a server request. However , there are some developers that doesn't want to pay , at less not yet for the service because you are only testing, don't have money or any other reason. The page geoplugin.net offers a free api to retrieve the country of the user that makes a request to our server.

How does it works

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.

Available information

We can retrieve some important information for free, the following keys are available in the object :

  • geoplugin_request : Contains the IP that we've just send in the IP get parameter.
  • geoplugin_status: The http status code of the request
  • geoplugin_city: The city of the request IP (if available)
  • geoplugin_countryCode: 2 chars country code (US,DE,RU)
  • geoplugin_countryName
  • geoplugin_continentCode
  • geoplugin_currencyCode: The money that the country handles (EUR,USD)
  • geoplugin_regionCode
  • geoplugin_regionName
  • geoplugin_areaCode
  • geoplugin_currencySymbol : Html symbol of the currency.

How to use it with PHP

With php the thing is really easy, we need to retrieve the IP of the request using the php native variable $SERVER which contains information about the request. Then we need to retrieve the information of the mentioned webpage (which actually will return a JSON Response but we need to convert to array using the json_decode function).

$ip = $_SERVER['REMOTE_ADDR']; // This will contain the ip of the request

// You can use a more sophisticated method to retrieve the content of a webpage with php using a library or something
// We will retrieve quickly with the file_get_contents
$dataArray = json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));

var_dump($dataArray);

// outputs something like (obviously with the data of your IP) :

// geoplugin_countryCode => "DE",
// geoplugin_countryName => "Germany"
// geoplugin_continentCode => "EU"

echo "Hello visitor from: ".$dataArray["geoplugin_countryName"];

How to use it with Javascript

With Javascript is a little bit complicated. First we need to retrieve the IP of the user of some way, the problem is that is not so easy to retrieve the IP with php, you need one way or another make a server call to retrieve the IP. If you can't use PHP because you have not a server or something, take a look to this question in StackOverflow, it can be possible to retrieve the ip adding a script that uses JSONP (a callback).

 Then we need to make a request to the mentioned url to get the country of the ip, but we will face the following issue if we simply make the request to the url:

XMLHttpRequest cannot load http://www.geoplugin.net/json.gp?ip=my.ip.number  No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mywebsiterequestdomain' is therefore not allowed access.

This problem is due to the CORS limitation, you cannot execute an ajax request to a server which is not yours. To resolve this problem we need to jump this limitation, we will use the cors-anywhere website to avoid this. Then our request will look like this: 

var ip = "the ip number, retrieved of some way by a server";

// the request will be to http://www.geoplugin.net/json.gp?ip=my.ip.number but we need to avoid the cors issue, and we use cors-anywhere api.

$.getJSON("https://cors-anywhere.herokuapp.com/http://www.geoplugin.net/json.gp?ip="+ip,function(response){
    console.log(response);
    // output an object which contains the information.
    alert("Hello visitor from "+ response.geoplugin_countryName);

});

Note: If you can and have a server to make request, then use the PHP way as it's easy and cleaner because we don't need to use the cors-anywhere.

Live example

The following fiddle allows you to retrieve the country from an ip with a simple form with javascript and jQuery.getJSON function. Go to the result tab an try it by yourself.

Have fun !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors