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....

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Beware of bugs in the above code; I have only proved it correct, not tried it.” - Donald Knuth