Click here to Skip to main content
Click here to Skip to main content
Go to top

Finding the geographical location of an IP address

, 26 Feb 2013
Rate this:
Please Sign up or sign in to vote.
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

<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

$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)

Share

About the Author

Shine Jacob (Enot)
Help desk / Support
United States United States
I love programming, learn about new technologies, algorithms, and problems solving using programming. I started off programming with C/C++. Though I learned PHP and web development but still stuck on C. Don't know why but I kinda love it.

Comments and Discussions

 
QuestionEye opener Pinmemberkenx2211-Mar-13 7:22 
AnswerRe: Eye opener PinmemberShine Jacob (Enot)6-Jun-13 23:59 
QuestionDuplicate PinmemberManish K. Agarwal29-Aug-12 20:12 
AnswerRe: Duplicate PinmemberShine Jacob (Enot)29-Aug-12 23:55 
GeneralRe: Duplicate Pinmemberkenx2211-Mar-13 7:25 
GeneralRe: Duplicate PinmemberShine Jacob (Enot)3-May-13 16:14 
GeneralMy vote of 4 PinmemberChristian Amado29-Aug-12 11:05 
GeneralRe: My vote of 4 PinmemberShine Jacob (Enot)29-Aug-12 14:54 
AnswerRe: My vote of 4 PinmemberChristian Amado29-Aug-12 17:30 
GeneralRe: My vote of 4 PinmemberShine Jacob (Enot)29-Aug-12 23:58 
GeneralThoughts PinmemberPIEBALDconsult29-Aug-12 6:01 
GeneralRe: Thoughts PinmemberShine Jacob (Enot)29-Aug-12 15:03 
GeneralRe: Thoughts PinmemberPIEBALDconsult30-Aug-12 3:51 
GeneralOK, a little misleading... Pinmemberkaschimer29-Aug-12 4:19 
GeneralRe: OK, a little misleading... PinmemberJohn Brett27-Feb-13 3:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 26 Feb 2013
Article Copyright 2012 by Shine Jacob (Enot)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid