Subsribe to our RSS

latest reactions

christian louboutin shoes
With that kind of traffic you want to ad …
Rahul Anand
Thanks for the nice post. It works for m …
Serkan
thx a lot! …
msb
Thanks for above solution.There is ano …
Lori S.
FYI, I was using this successfully in CF …

Use OpenDNS

mxna feeds

cfmailparam behaviour change in CF9.01ColdFusion Job Opportunity in Houston, TXFacial Recognition in 14 Lines Of ColdFusionColdFusion UPS PackageColdFusion Job Opportunity in Arlington, VAColdFusion Job Opportunity in San Diego, CASome ColdFusionBloggers.org GuidelinesColdFusion Positions In CaliforniaListChangeDelimsunix epoch with coldfusionQuery scripting bug in Coldfusion 9Scoth On The Rock 2011 Tickets on salefoursquareCFC updated to version 1.0, now includes ColdFusion return typeFramework One Birds of a Feather session at CFUnitedWhat makes you a good ColdFusion programmer?

All files are strictly confidential: all information is classified.
© Copyright 2002 - 2010 mximize.com.
All right reserved.

MXNA webfeed

Visit Carlos Gallupa

Getting country by IP based on GeoLite

For a global application I needed some way to determine the country of the visitor. A couple of years ago I used a similar solution but that database was not available anymore. The guys at Maxmind however publish a free, yes free geoIP database every month and according to them it has an accuracy of 98% procent.

So what I did was rewrite my original component so now it is speaking to this new database from Maxmind. Here are the steps to make this work for you!

First download the geoIP Lite datbase from Maxmind. (CSV format)
Import this into your favorite database, mine is MSSQL. Maxmind has different links to examples on how to do this.

Name the table "geoip" and the columns "begin_ip,end_ip,begin_num,end_num,country,name", and make sure that if you are using MSSQL that the _num columns are of datatype BIGINT.

After that copy/paste the following component and save this as geoip.cfc

<cfcomponent>
    <cffunction name="ip2country" access="remote" returntype="query">
        <cfargument name="ip" required="yes" type="string">
        <cfset var q = "">

        <cfset Aip = ListToArray(arguments.ip,'.')>
        <cfset Aip = ((Aip[1] * 16777216) + (Aip[2] * 65536) + (Aip[3] * 256) + (Aip[4]))>

        <cfquery name="q" datasource="-- YOUR DSN ---">
            SELECT country,name
            FROM geoip
            WHERE begin_num <= <cfqueryparam cfsqltype="CF_SQL_BIGINT" value="#Aip#">
            AND end_num >= <cfqueryparam cfsqltype="CF_SQL_BIGINT" value="#Aip#">
        </cfquery>
       
        <cfreturn q>
    </cffunction>
</cfcomponent>


After that make a test page with this code in it. If all goes well it should display the country that your are currently in.

<cfinvoke component="geoip" method="ip2country" returnvariable="qCountry">
    <cfinvokeargument name="ip" value="#cgi.remote_addr#" />
</cfinvoke>

<cfdump var="#qCountry#">


To make it a bit easier to import this database I have made a download available for you guys and gals in a normal TXT format. A what the heck.. the complete code is in the download.

6455 viewed | 3 opinion(s)  | del.icio.us | Digg it | Tjarko @ 20/06/07 11:01 cet


Reactions:

jax wrote....

Cool way to define the ip ranges in the database!

20 June 2007 13:09 cet  

Tim wrote.... (site)

We are using similar technique for the IP2Location database and it works well.

09 January 2008 14:34 cet  

Donald Boer wrote.... (mail)

I tried it but don't get Country and Name displayed, Jut the cfdump header.

27 December 2009 11:23 cet  

Leave your comment

Your name


Your url/website/link/email....


Some room for your reaction is placed here..



The answer to the ultimate question is?? (42 ;-))




URL en mail addresses are translated for you... life sometimes is that simple!!