Library tutorials & articles
Extracting the Country from the IP Address:- How To?
By Amit Gupta, published on 13 Mar 2004
Get Coding
Now that we've got the database, we will move on to writing the ASP code.
Firstly, we must know how to get the IP Address of the Visitor. To do that, our code will be
The ServerVariables collection contains the combination of values that represent the HTTP headers sent from the visitor's browser to our server. By passing the " HTTP_REMOTE_ADDR" parameter to it, we request the IP Address of the visitor.
Through this, we will get an IP Address in the format xxx.xxx.xxx.xxx which may look like 108.89.128.147
But since our database have IP Numbers instead of IP Addresses, we will have to convert the IP Address into an IP Number.
To convert an IP Address into an IP Number, the formula is
A * (256*256*256) + B * (256*256) + C * 256 + D
for the IP Address A.B.C.D
So, for ease of things, we can use the following function to get an IP Number of an IP Address.
so, we can use this function as
Thus we will get the IP Number of our IP Address. Now we are ready to query our ip2country database.
Firstly, we must know how to get the IP Address of the Visitor. To do that, our code will be
|
<% strIP = Request.ServerVariables("HTTP_REMOTE_ADDR") %> |
The ServerVariables collection contains the combination of values that represent the HTTP headers sent from the visitor's browser to our server. By passing the " HTTP_REMOTE_ADDR" parameter to it, we request the IP Address of the visitor.
Through this, we will get an IP Address in the format xxx.xxx.xxx.xxx which may look like 108.89.128.147
But since our database have IP Numbers instead of IP Addresses, we will have to convert the IP Address into an IP Number.
To convert an IP Address into an IP Number, the formula is
A * (256*256*256) + B * (256*256) + C * 256 + D
for the IP Address A.B.C.D
So, for ease of things, we can use the following function to get an IP Number of an IP Address.
| <% Private Function ipAd2ipNum(ipA) strO = ipA pos1 = InStr(strO, ".") intA = CInt(Left(strO, (pos1-1))) strO2 = Mid(strO, pos1+1, len(strO)) pos2 = InStr(strO2, ".") intB = CInt(Left(strO2, (pos2-1))) strO3 = Mid(strO2, pos2+1, len(strO2)) pos3 = InStr(strO3, ".") intC = CInt(Left(strO3, (pos3-1))) intD = CInt(Mid(strO3, pos3+1, len(strO3))) intConvert = (intA*(256*256*256)) + (intB*(256*256)) + (intC*256) + intD |
so, we can use this function as
| <% strIP = Request.ServerVariables("HTTP_REMOTE_ADDR") strIPN = ipAd2ipNum(strIP) %> |
Thus we will get the IP Number of our IP Address. Now we are ready to query our ip2country database.
Related articles
Related discussion
-
Read eMails from Outlook express using ASP
by kumaravelu (1 replies)
-
Help to Call ASP function from onclick event in HTML to pass an array
by vka (0 replies)
-
Binary Studio | software development outsourcing Ukraine
by shane124 (4 replies)
-
Variable In Vb.Net
by chia (0 replies)
-
ideas in building a captive portal
by sjranjan (2 replies)
Related podcasts
-
Scott Guthrie
Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.
Thanks for the comment..i really like this..
Get your Term papers done - FlashPapers.com
!--removed tag-->You can check Country, City, ZIP, Longitude and Latitude in C# and visual basic (VB.NET). There is source code for .NET GeoIP: http://tools.webmastermafia.com
You can use it for FREE. It is very easy, just 2 lines of code.
!--removed tag-->thanks a lot...it would me more useful if you post it in VB.NET..or C# ..thanks in advance
!--removed tag-->I just wrote a lasted sample which describes how to get countries and city from IP address and the sample contains a IP database.If you want to get the sample code,Please visite http://www.treaple.com/bbs/index.php or http://www.treaple.com/bbs/thread-17-1-1.html
First of all,you should get a database which includes all countries IP details.
and then convert IP address to decimal to find the exact country name by the decimal .
I hope that this article can be used for you
http://www.treaple.com/ArticlesDetails.aspx?id=55
cheer.
Though this is a brilliant piece of code, can u guys please explain that what are the datatypes used in the function ipAd2ipNum and also it gives me an error if i use the datatypes like integer, int32, int64...Please reply asap Rohit![Smiley Face [:)]](/emoticons/emotion-1a.gif)
Thanks a lot.![Smiley Face [:)]](/emoticons/emotion-1a.gif)
![Crying [:'(]](/emoticons/emotion-9.gif)
Hi,
First i woud like to thank you fom the bottom of my heart, on this wounderful, well orginized article, it really helped me a lot, i have been looking for a one like this for a long time
THANX A LOT
btw, i guese you have forgotten about the database anyway,
i'll do u simple favor for you and others as little return for this article
the link for the latest CSV Values
http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip
Once again, Thanx a lot
Best Regards
How to download the MS Access database?
If your wondering why "strIP = Request.ServerVariables("HTTPREMOTEADDR")" returns no IP address change it too "strIP = request.servervariables("REMOTE_ADDR")" and this will now work !
This thread is for discussions of Extracting the Country from the IP Address:- How To?.