If you wish to determine a remote visitor's country, city, state, postal code and other location information, I'd personally recommend integrating with a web service which maintains a current database.

Our service of choice is MaxMind's GeoIP Web Service.

Integration is a snap with a simple HTTP GET request. ColdFusion example code is provided below:

<cffunction name="locationInfo" access="public" output="false" returntype="struct">
    <cfset locationDict = StructNew()>

    <cftry>
        <cfset ip = cgi.REMOTE_HOST>
   
        <cfhttp method="get" url="geoip3.maxmind.com/f?l=D90rjl5Oq9km&i=#ip#" result="info"></cfhttp>
       
        <cfset info = info.Filecontent>
   
        <cfset locationDict.country = ListGetAt(info,1)>
        <cfset locationDict.state = ListGetAt(info,2)>
        <cfset locationDict.city = ListGetAt(info,3)>
        <cfset locationDict.postal = ListGetAt(info,4)>
    <cfcatch>
        <cfset locationDict.country = "">
        <cfset locationDict.state = "">
        <cfset locationDict.city = "">
        <cfset locationDict.postal = "">       
    </cfcatch>
    </cftry>

    <cfreturn locationDict>
</cffunction>

Note the error handling, which gracefully returns an empty string for all values, in the event the web service is down, or has change it's response format.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *