Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / Apache

Finding the geographical location of an IP address

4.56/5 (5 votes)
26 Feb 2013CPOL3 min read 44.3K  
A tip on how to find the geographical location of an IP address.

Introduction

We sometimes need to find out the location of an IP address so that we can utilize it in our website in some way. This article will show you how to obtain the location of an IP address through the API provided by locatorhq.com. The usage of this API is quite simple and free to use. I will show you how to use the API and implement the code on a webpage.

How to obtain an API key?

First of all, you need to visit http://www.locatorhq.com and sign up with your name and email address. Don't worry, it's free. An API key will be sent to your email address with your username. Once you have that information, you can start using the API.

How to use the API?

This is how a basic locatorHQ API call looks like:

http://api.locatorhq.com/?user=YOURUSERNAME&key=YOURAPIKEY&ip=IPADDRESSTOLOOKUP

Here:

user = your username registered with the website
key  = your API key
ip   =  ip address to look up

The other parameter is format with which you can specify the format in which you want the output in.

http://api.locatorhq.com/?user=YOURUSERNAME&key=YOURAPIKEY&ip=IPADDRESSTOLOOKUP&format=xml

Or

http://api.locatorhq.com/?user=YOURUSERNAME&key=YOURAPIKEY&ip=IPADDRESSTOLOOKUP&format=text

Or

http://api.locatorhq.com/?user=YOURUSERNAME&key=YOURAPIKEY&ip=IPADDRESSTOLOOKUP&format=json

As of now, the JSON format is not yet available I guess. If you don't use the format parameter, the API dumps the output in TEXT format.

Let's take an example here, let's look up for the location for Google's IP address. Type http://api.locatorhq.com/?user=Username&key=APIKEY&ip=74.125.236.196 into your browser.

Here is the output:

US,United States,California,Mountain View,34.305,-86.2981

Here is the breakup of the output:

Country CodeUS
CountryUnited States
RegionCalifornia
CityMountain View
Latitude34.305
Longitude-86.2981 

Now hope you're with me so far. We will see how to use the XML format.

Here is how you mention the format: http://api.locatorhq.com/?user=Username&key=APIKEY&ip=74.125.236.196&format=xml

XML
<ip2locationapi><countryCode>US</countryCode>
<countryName>United States</countryName><region>California</region><city>Mountain View</city>
<lattitude>34.305</lattitude><longitude>-86.2981</longitude></ip2locationapi>

Alright, now we know how to use the API so we're ready to use it on our webpage using PHP. So here is the PHP code that will tell the visitor of the webpage about his/her location:

PHP
<?php

$ipaddress = $_SERVER['REMOTE_ADDR']; //ip address
$locationstr="http://api.locatorhq.com/?user=myusername&key=myAPIkey&ip=";
$locationstr = $locationstr.$ipaddress."&format=xml";

//loading the xml file directly from the website
$xml = simplexml_load_file($locationstr); 

$countrycode = $xml->countryCode; //country code
$countryname = $xml->countryName; //country name
$region = $xml->region; //region name
$city = $xml->city; //city name
$lattitude = $xml->lattitude; //city latitude
$longitude = $xml->longitude; //city longitude
//$browsername = $xml->browserName; //browser name


echo "Location information for <b>".$ipaddress."<br/><br/>";
echo "Country Code: ".$countrycode."<br/>";
echo "Country: ".$countryname."<br/>";
echo "Region: ".$region."<br/>";
echo "City: ".$city."<br/>";
echo "Lattitude: ".$lattitude."<br/>";
echo "Longitude: ".$longitude."<br/>";
?>

How is this useful to me?

Well, you can actually use this in any way you wish and you can use your imagination but here are some common uses:

  • You can personalize the webpage and present it in the language from where the visitor is from. 
  • You can use this information to monitor the web traffic coming to your website. This helps you customize the website and allows you to present the products that sells more in that region.
  • If you have advertisement on your website then you can present those advertisements that are applicable and more popular in the region from where the visitor is from.
  • You can help the visitor with the nearest stores and service center locations with this information, which avoids a lot of hassle for the customer searching for this information. 
  • With regards to security, for example, your system administrator is aware of a hacker's group in particular area set their minds to deface any website on their way. You can use this information to block those IP addresses if something suspicious is happening.

There are lot more things you can do with this information.

Points of interest

When I first saw this thing and used it, it was simply amazing for me. I really like this so I decided to share this information. I know many people out there might know about this already but for those who don't, start using it. It is simply awesome. I am no expert in anything, so your comments and suggestion are valuable to me.

Thanks for reading.

If you have any questions or suggestions, please email to me at shine_hack@yahoo.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)