This blog has moved to http://www.andyjarrett.co.uk/blog/ | New RSS FEED

geo based IP lookup with ColdFusion

I've been looking around for a bit for a free location lookup based on an IP address. Preferably as an API call so I don't need to install/add too much my end.

I've come across hostip.info. This service is a community-based project with around 8,542,587 entries in the database!

They have a good API which supplies text and XML results, and they even have the database freely available for download with daily updates.

Below is a quick example of some code to get the country (you can find more examples at the site). If you want to see it in action click here.


<cfhttp url="http://api.hostip.info/?ip=#cgi.remote_host#" method="get" result="geoInfo" />

<cfscript>
    geoXml = xmlParse(geoInfo.fileContent);
    country = geoXml.HostipLookupResultSet['gml:featureMember']['hostip']['countryname'].xmlText;
    countryAbbrev = geoXml.HostipLookupResultSet['gml:featureMember']['hostip']['countryAbbrev'].xmlText;
</cfscript>

<cfoutput>Your IP address is: #cgi.remote_host#<br/>
Your country is: #country#    (#countryAbbrev#)
</cfoutput>    
<hr/>
Full dump
<cfdump var="#geoXml#"    />

For more info/help see also:

*** Comments *** (*** Comment Moderation is enabled. Your comment will not appear until approved. ***)
Shane Zehnder's Gravatar I have used hostip.info for quite some time in WhosOnCFC (thanks to Joshua Cyr). It does a pretty good job, but for a free service you definitely cannot complain.
# *** Posted By *** Shane Zehnder | 9/13/08 8:30 PM
PaulH's Gravatar i think you'll find that it's webservice or even a straight lookup from it's d/l db won't scale very well. you'll be better off with a local binary "db" like our geoLocator http://www.sustainablegis.com/projects/geoLocator/... (which unfortunately doesn't get updated as frequently as we like) or maybe maxmind's.
# *** Posted By *** PaulH | 9/13/08 8:55 PM
Ricardo's Gravatar Andy, very good article.
If you don't mind, I translated it to Portuguese and posted in our Brazilian blog http://porqueCF.com.br/blog (that means: why CF?). Of course I gave you all the credits and set links to your blog. Let me know if that was ok.
I also added yours to my "preferred blogs " session in http://ricardo.parente.us.
Thanks.
Ricardo
# *** Posted By *** Ricardo | 9/16/08 8:33 PM
Andy Jarrett's Gravatar @Ricardo thats fine :) Thanks for the heads up though
# *** Posted By *** Andy Jarrett | 9/17/08 1:44 AM
Jeff's Gravatar I always use http://www.ipgp.net because it is the most accurate and it gives a map too.
# *** Posted By *** Jeff | 11/1/08 6:47 AM
BlogCFC was created by Raymond Camden / Contact Blog Owner / mptooling.com / spicemerchants-portsmouth.co.uk / ipicture.it