Click here to Skip to main content
13,864,078 members
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 29 Aug 2012
Licenced CPOL

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.


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 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 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:


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.



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 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
CityMountain View

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

Here is how you mention the format:

<countryName>United States</countryName><region>California</region><city>Mountain View</city>

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:


$ipaddress = $_SERVER['REMOTE_ADDR']; //ip address
$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


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


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.

You may also be interested in...

Comments and Discussions

QuestionEye opener Pin
kenx2211-Mar-13 8:22
memberkenx2211-Mar-13 8:22 
AnswerRe: Eye opener Pin
Shine Jacob (Enot)7-Jun-13 0:59
memberShine Jacob (Enot)7-Jun-13 0:59 
QuestionDuplicate Pin
Manish K. Agarwal29-Aug-12 21:12
memberManish K. Agarwal29-Aug-12 21:12 
AnswerRe: Duplicate Pin
Shine Jacob (Enot)30-Aug-12 0:55
memberShine Jacob (Enot)30-Aug-12 0:55 
GeneralRe: Duplicate Pin
kenx2211-Mar-13 8:25
memberkenx2211-Mar-13 8:25 
GeneralRe: Duplicate Pin
Shine Jacob (Enot)3-May-13 17:14
memberShine Jacob (Enot)3-May-13 17:14 
GeneralMy vote of 4 Pin
Christian Amado29-Aug-12 12:05
professionalChristian Amado29-Aug-12 12:05 
GeneralRe: My vote of 4 Pin
Shine Jacob (Enot)29-Aug-12 15:54
memberShine Jacob (Enot)29-Aug-12 15:54 
AnswerRe: My vote of 4 Pin
Christian Amado29-Aug-12 18:30
professionalChristian Amado29-Aug-12 18:30 
GeneralRe: My vote of 4 Pin
Shine Jacob (Enot)30-Aug-12 0:58
memberShine Jacob (Enot)30-Aug-12 0:58 
GeneralThoughts Pin
PIEBALDconsult29-Aug-12 7:01
protectorPIEBALDconsult29-Aug-12 7:01 
GeneralRe: Thoughts Pin
Shine Jacob (Enot)29-Aug-12 16:03
memberShine Jacob (Enot)29-Aug-12 16:03 
GeneralRe: Thoughts Pin
PIEBALDconsult30-Aug-12 4:51
protectorPIEBALDconsult30-Aug-12 4:51 
GeneralOK, a little misleading... Pin
kaschimer29-Aug-12 5:19
memberkaschimer29-Aug-12 5:19 
GeneralRe: OK, a little misleading... Pin
John Brett27-Feb-13 4:21
memberJohn Brett27-Feb-13 4:21 

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

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190214.1 | Last Updated 26 Feb 2013
Article Copyright 2012 by Shine Jacob (Enot)
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid