I had a requirement to find out Country and set it to a dropdown control. For this I needed to collect the IP of the user and secondly match the IP collected with a database of IP's.
1. Collect the IP of the user. Two IP's are available - one of the proxy's and other the users machines. We must deciede to collect either the proxy IP address or the the machine ip of the user
To collect the IP Address of the Proxy we can use HttpContext.Current.Request.UserHostAddress
To Collect the IP of the user machine(not the proxy server ip),
HttpContext.Current.Request.ServerVariables("HTTP_X_FORWARDED_FOR")
2.Match the collected IP address to an IP Database
As we all know an IP address is in the format xx1.xx2.xx3.xx4.
a)IP Address Table
The IP address table is available as free downloads as text or MS Access DB, contains the following fields:
StartIP,
EndIP,
StartIP as a equivalent number,
EndIP as an equivalent number,
CountryCode and
Country.
If we deciede to keep this table as a table in our database, it needs to be a different table (not to integrate with existing country table), since there are many IP address rows per country.
b) Convert IP Address to its Equivalent Number
We have to convert the collected IP to its equivalent number. The following formulae will convert to its equivalent number - (xx1*16777216) +(xx2*65536)+(xx3*256) +xx4
c) Match the equivalent number of converted IP to that of the IP Database
Match the following formulae to get its equivalent country.
If lngConvertedIP between NumberEquivalentOfStartIP and NumberEquivalentOfEndIP, retrieve countryCode.
No comments:
Post a Comment