MUS
MUS

Reputation: 1450

Get visitor country info using classic ASP 3.0

I need to get visitors country information using classic asp 3.0, i'm using below code but it returns me XX instead of country name. any suggestions/help on this.

<%
        URL = "http://api.hostip.info/country.php?ip=" & Request.ServerVariables("REMOTE_ADDR")
        Set conn = Server.CreateObject("MSXML2.ServerXMLHTTP")
        conn.open "GET", URL, False, "", ""
        conn.send
        UserCountry = conn.ResponseText        
        Response.Write(UserCountry)

%>

i have also setup a page for view which contains the above code.

http://www.datingmaze.co.uk/rac.asp

If i try http://api.hostip.info/country.php?ip=12.215.42.19 it gives me correct output, but if i tried http://api.hostip.info/country.php?ip=119.152.136.247 gives me wrong output i.e. XX although i provided the correct IP address.

thanks in advance.

Upvotes: 1

Views: 2796

Answers (1)

Robin Orheden
Robin Orheden

Reputation: 2764

You can use GeoIP for this.

They have a free COM API which you can use:

<%
if Request.Form("values") = "Test Values" then
hostname = "www.yahoo.com"
else
    hostname = Request.Form("hostname")
end if

if Request.Form("submit") = "Submit" then

    set geoip = Server.CreateObject("GeoIPCOMEx.GeoIPEx")

    geoip.set_db_path("C:\Program Files\GeoIP\")
geoip.find_by_name(hostname)
city = geoip.city

    Response.Write("<table cellpadding=2 border=1><tr><th colspan=2>Results</th></tr>")
    Response.Write("<tr><td>Hostname</td><td>" + hostname + "</td></tr>")
    Response.Write("<tr><td>GeoIP City Value</td><td>" + city + "</td></tr>")
    Response.Write("</table>")
end if
%>

http://www.maxmind.com/app/com

http://www.maxmind.com/GeoIP-COM-1.3.zip

Their COM API exposes the following:

Methods:
bool set_db_path(string path) (must be set before any other operations, true if all dbs found)
bool find_by_addr(string ipvalue) (return true if address found, sets all properties)
bool find_by_name(string dns_name) (-"-)

The data that you would receive after lookup:

Properties:
country_code (2 chars; "LN" if non-routed addr, "LH" if localhost)
country_code3 (3 chars)
country_name ("Local Area Network" if non-routed addr,"Localhost" if localhost)
region    (2 chars, state abbrev for US/Canada, FIPS 10-4 region code for others)
city
postal_code (max 6 chars, US and Canada only)
latitude  (real number)
longitude (real number)
dma_code  (integer)
area_code (integer)

So instead of the using find_by_name, you would use find_by_addr. Which would lookup the country based on an IPv4-address.

This is a better solution since relying on remote 3rd party providers can be risky. Their site might go down, be under heavy load, etc.

You can download the free version of their IP/Country database here:

http://www.maxmind.com/app/geolitecountry

Upvotes: 2

Related Questions