latest reactions

spider
One way is to google for page which cont …
andy
you can find this tool here http://andy. …
Tjarko
Eej Walker, that's pretty cool, I will c …
Walker
Thanks for posting your thoughts. I modi …
Albert
Thanks !! …

Clicky Web Analytics
Use OpenDNS
ZERIGO DNS

Ultimate dns hosting based on pre-defined templates, DNS redirection and lot's more.

mxna feeds

CF101 Archive: September 2007 Object Oriented PizzaJob Opening for ColdFusion Developer at Arizona State UniversityMy New Experience Getting ColdFusion Builder 2 Console WorkingCF101 Archive: February 2007 Data Table GatewaysJob Opening for Sr. ColdFusion Developer in CA/ San Fernando ValleyColdFusion Supports HTTP Verbs PUT And DELETE (As Well As GET And POST)Best wishes for the new FuseboxCF101 Archive: January 2007 Data Access ObjectsOpen Source Conference For CFML DevelopersSkype Blocking Web Services with IIS, ColdFusion & RailoCF101 Archive: October 2006 Five Cool Things with ColdFusionGenerate a tag cloud from an RSS feed with ColdFusionColdFusion MeetUp: Cloudy with a Chance of Tests, with Mike HenkeReminder - Adobe CookbooksCF101 Archive: September 2006 A first look at FusionDebug

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

Visit Carlos GallupaMXNA webfeed

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.

8329 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!!