Library tutorials & articles

Extracting the Country from the IP Address:- How To?

The Full Code

So, after doing bits of code, here's the full code for our application:-

<% @language="VBScript" %>
<%
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
ipAd2ipNum = Trim(intConvert)
End Function

 

strIP = Request.ServerVariables("HTTP_REMOTE_ADDR")
strIPN = ipAd2ipNum(strIP)

 

strConString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
strConString = strConString & Server.MapPath("ip2country.mdb")

Set objCon = Server.CreateObject("ADODB.Connection")
objCon.Open strConString

strSQL = "select co_name from ip2c where ip_from<=" & strIPN
strSQL = strSQL & "and ip_to>=" & strIPN

Set objRs = Server.CreateObject("ADODB.Recordset")
objRs.Open strSQL,objCon
If NOT(objRs.EOF) OR NOT(objRs.BOF) Then
strCountry = objRs.Fields("co_name")
%>
Your IP Address is :- <%=strIP%><br>
Your Country is :- <%=strCountry%>
<%
Else
%>
Your IP Address is :- <%=strIP%><br>
Your Country is :- <b>unlisted</b>
<%
End If

objRs.Close
objCon.Close
Set objRs = nothing
Set objCon = nothing
Set strSQL = nothing
%>



This wraps up our tutorial Extracting the Country from the IP Address:- How To?

But a few final words....

Comments

  1. 20 Nov 2009 at 06:04

    Thanks for the comment..i really like this..

    Get your Term papers done - FlashPapers.com

  2. 19 Nov 2009 at 12:49

    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.

  3. 08 Sep 2009 at 16:34

    thanks a lot...it would me more useful if you post it in VB.NET..or C# ..thanks in advance

  4. 08 Sep 2007 at 19:46

    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

  5. 02 Sep 2007 at 18:06

    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   .

  6. 02 Sep 2007 at 18:02

    I hope that this article can be used for you

    http://www.treaple.com/ArticlesDetails.aspx?id=55

    cheer.

  7. 03 May 2007 at 14:37

    Hi Guys,

    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 [:)]

  8. 11 Nov 2006 at 16:43

    Thanks a lot.Smiley Face [:)]Crying [:'(]

  9. 06 Jun 2006 at 19:07

    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

  10. 14 Nov 2005 at 12:30

    How to download the MS Access database?

  11. 28 Oct 2005 at 18:42

    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 !

  12. 01 Jan 1999 at 00:00

    This thread is for discussions of Extracting the Country from the IP Address:- How To?.

Leave a comment

Sign in or Join us (it's free).

Amit Gupta

We'd love to hear what you think! Submit ideas or give us feedback