Skip To Main Content | Home | Sitemap | Contact
Latest Visitors: United States's flagUnited States's flagUnited States's flagUnited States's flagUnited States's flagUnited States's flagNetherlands's flagChina's flagSweden's flagSerbia's flagChina's flagChina's flagUnited States's flagCanada's flagUnited States's flagUnited Kingdom's flagKorea  Republic of's flagUnited States's flagUnited States's flagChina's flagUnited Kingdom's flagUnited States's flagEurope's flagChina's flagSweden's flag

Geo-Coding IP Addresses

Rod Divilbiss
2-6-2006
Updated: 11-17-2009

"Cities, like cats, will reveal themselves at night."
                                           Rupert Brook

 

It seems to have become increasingly popular to display the city and country of the browser visiting your web site in the web page.  Obtaining the city, country and/or coordinates on the globe from an IP address is called geo-coding.

There are several commercial sites that expose a web service or application programming interface (API) for you to get the geographic location of an IP address. This simple tutorial will show a quick and simple method to do this using a custom object written in ASP and the GPL "hostip" database maintained at http://ipinfodb.com/.

If you use the service, please consider a contribution, of any amount, to help defray the cost of hosting and maintaining the database. You can imagine what the bandwidth expenses must be for a popular database such as this.

To use the ASP class in your page you need only to add one line to your pages code.

browserLocale Include
<!--#include file="browserLocale.asp"-->

The "browserLocale" object is automatically created on page load and its properties loaded with the geo-code information corresponding to your visitors IP address.  The eleven properties exposed by the browserLocale object are;

  1. .ip
  2. .countryCode
  3. .countryName
  4. .regionCode
  5. .city
  6. .zipPostalCode
  7. .latitude
  8. .longitude
  9. .timeZone.
  10. GMTOffset.
  11. DSTOffset

To write your visitor's city and country to the page you simply write the city and country properties.


Write The Vistor's Country
<%
Response.Write browserLocale.city & " " & browserLocale.country
%>

There is no coding necessary on your part. There is no subscription to buy. You need no registration or access key. Just include one line at the top of your page and use the five properties as needed.

The class containing the browserLocale object is only a few lines of code and uses remote scripting to access the hostip.info's api. A tiny XML file is returned from hostip.info and is parsed into the properties and made available for use. The browserLocale object also has one method, ".spill" which will write all five properties to the browser in a table.  The listing below is an example page using the spill method which is useful for testing the object.

Example browserLocale Page
<!--#include file="getBrowserLocale.asp"-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="Roderick Divilbiss">
<meta name="copyright" content="© 2005-2010 Roderick Divilbiss">
<title>Geocode IP</title>
</head>

<body>
<p><% browserLocale.spill %></p>
</body>

</html>

And finally, the browserLocale class file.

browserLocale Class
<%
class browserLocaleObject
    public ip
    public countryCode
    public countryName
    public regionCode
    public city
    public zipPostalCode
    public latitude
    public longitude
    public timeZone
    public GMTOffset
    public DSTOffset


    Private sub Class_Initialize
        Dim objXMLHTTP
        Dim coordinates
        Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
        ip = Request.ServerVariables("REMOTE_ADDR")
        objXMLHTTP.Open "GET", "http://ipinfodb.com/ip_query.php?ip="&ip, False
        objXMLHTTP.Send
        Set xmldoc = objXMLHTTP.responseXML
        on error resume next
        countryCode = xmlDoc.documentElement.selectSingleNode("CountryCode").text
        countryName = xmlDoc.documentElement.selectSingleNode("CountryName").text
        regionCode = xmlDoc.documentElement.selectSingleNode("RegionCode").text
        city = xmlDoc.documentElement.selectSingleNode("City").text
        zipPostalCode = xmlDoc.documentElement.selectSingleNode("ZipPostalCode").text
        latitude = xmlDoc.documentElement.selectSingleNode("Latitude").text
        longitude = xmlDoc.documentElement.selectSingleNode("Longitude").text
        timeZone = xmlDoc.documentElement.selectSingleNode("Timezone").text
        GMTOffset = xmlDoc.documentElement.selectSingleNode("Gmtoffset").text
        DSTOffset = xmlDoc.documentElement.selectSingleNode("Dstoffset").text
        set objXMLHTTP=nothing
    End sub

    Public Sub Spill
        Dim out
        out = "<table>"
        out = out & "<tr><td width=""50%"">IP</td><td width=""50%"">" & ip & "</td></tr>"
        out = out & "<tr><td width=""50%"">Country Code</td><td width=""50%"">" & countryCode & "</td></tr>"
        out = out & "<tr><td width=""50%"">Country Name</td><td width=""50%"">" & countryName & "</td></tr>"
        out = out & "<tr><td width=""50%"">Region Code</td><td width=""50%"">" & regionCode & "</td></tr>"
        out = out & "<tr><td width=""50%"">City</td><td width=""50%"">" & city & "</td></tr>"
        out = out & "<tr><td width=""50%"">ZipPostalCode</td><td width=""50%"">" & zipPostalCode & "</td></tr>"
        out = out & "<tr><td width=""50%"">Latitude</td><td width=""50%"">" & latitude & "</td></tr>"
        out = out & "<tr><td width=""50%"">Longitude</td><td width=""50%"">" & longitude & "</td></tr>"
        out = out & "<tr><td width=""50%"">Timezone</td><td width=""50%"">" & timeZone & "</td></tr>"
        out = out & "<tr><td width=""50%"">GMT Offset</td><td width=""50%"">" & GMTOffset & "</td></tr>"
        out = out & "<tr><td width=""50%"">DST Offset</td><td width=""50%"">" & DSTOffset & "</td></tr>"
        out = out & "</table>"
        Response.Write out
        End Sub
End class

Dim browserLocale
Set browserLocale = new browserLocaleObject
%>
 

With a one-line include you have your visitor's IP address geo-coded.  Note it is simple to provide PHP and JavaScript versions of this object and I hope to post those tutorials soon.  Feel free to help yourself to the source code and examples at the link below.

Source for the browserLocale class and examples.


Other Remote Scripting Articles By This Author

AJAHT (AJAX isn't just for XML)
Including Files with JavaScript (No IFRAME, No Hacks)
Remote Scripting Images !AJAX
Please Wait! (Letting the user know an AJAX call is pending)
HTTP communications with a Server
Reading A Web Server's Response Headers




This Weeks Most Popular Pages Newest Pages