Open Source Ip Geographical Location
Posted on April 2, 2007 by Guy Snir
Filed Under Open Source, .NET |
I was searching for a way to geographically locate my website visitors, this is usually used for better personalization of your website, regulatory compliance, things of that sort.
I found some very nice piece of Open Source code at MaxMind’s website.
They have very useful API’s (available for C, Perl, PHP, Apache Module, Java, Python, C#, Ruby, etc.) that do exactly what I was looking for.
There is also a DB available that is updated monthly to lookup the countries.
I needed a C# API so I downloaded the “C# API with Visual Studio project file for GeoIP Country only”, the sample code worked like a charm the first time, I tested it a bit (with my IP and IP’s from around the world) and it seems pretty accurate.
I am now in the process of incorporating the API into our software, some changes where necessary in the code (like removing Console.WriteLine outputs)
The use of the API is fairly simple.
I used some ASP.NET code to get the IP address:
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]
Then you use the API:
CountryLookup cl = new CountryLookup(@"GeoIP.dat");
cl.lookupCountryName("The IP addess")
That is about it.
Happy locationing!
Comments
2 Responses to “Open Source Ip Geographical Location”
Just what i looked for!
i had this problem of locating my website visitors but haven’t found decent tool for it.
thanks!
Avi.
Drew…
I guess it’s ok…