There are situations where we have to track visitors. The tracking might include the country from the visitor has browsed the website. These statistics are often important.
This project provides a library to resolve the country name given an IP. The project uses the Codeplex IPExtensions projects. This project has embedded information of the various ip address ranges assigned to different countries.
The major advantage of this method is a single DLL. It does not require any database setup etc. You just need to add reference to the IpToCountryLib DLL provided in the solution.
To resolve the country name, the library API to be used is "GetCountry". GetCountry takes in a string parameter which is the IP Address and returns the country name as the out parameter.
This is the code of the function, C#
public bool GetCountry(string userHostIpAddress, out string countryName) { bool result = false; countryName = string.Empty;
if (string.IsNullOrEmpty(userHostIpAddress)) return false;
IPAddress ipAddress; if (IPAddress.TryParse(userHostIpAddress, out ipAddress)) { countryName = ipAddress.Country();
result = true; }
return result; }
VB,
Public Function GetCounTry(ByVal userHostIpAddress As String, ByRef counTryName As String) As Boolean Dim result As Boolean = False counTryName = String.Empty If String.IsNullOrEmpty(userHostIpAddress) Then Return False End If Dim ipAddress As IPAddress If IPAddress.TryParse(userHostIpAddress,out ipAddress) Then counTryName = ipAddress.CounTry() result = True End If Return result End Function
To use the function you will do a call like this, C#,
IPToCountry _lib = new IPToCountry(); string country; _lib.GetCountry("111.111.111.111",out country);
VB.Net,
Dim _lib As IPToCounTry = New IPToCounTry() Dim counTry As String _lib.GetCounTry("111.111.111.111",out counTry)
I have attached the source code and also an exe for the application. The application attached contains a WPF application using the library and demonstrating the feature.
Have fun.
AttachmentsSource Code (663-262226-IPToCountry.rar)Exe (663-262226-EXE.rar)
|
Author: Kritika Yadav | Member Level: Gold | Revenue Score: |
hey really nice code. do you have any code to get domain from where we reached the current page.
|
Author: premkumar | Member Level: Silver | Revenue Score: |
how can i implement this in my application...i tried am not getting anything.
|
Author: Pritom Nandy | Member Level: Gold | Revenue Score: |
Indeed a very good article and effective as I needed it on my project.
Thanks
|
Author: Johney Joseph | Member Level: Bronze | Revenue Score: |
hi, very nice to post such a project. this's a most wanted snippet needed by many serious developers.
|
Author: Neeraj Kumar SIngh | Member Level: Silver | Revenue Score: |
hi, nice project. how we run this project? how we use it in our application?
|
Author: siva | Member Level: Gold | Revenue Score: |
Hi, Its really nice. I would like learn such type of things.
It will be greatful for me to send the files.
Thanks Siva Sreenath
|