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

Running Query

As we've got the IP Number of the visitor's IP Address, we will now query the ip2country database to find out the Country of the visitor.
For this, first we connect to our ip2country database.

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

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

The connection to database is now open. We will now create the query & run it.

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

after running the query, we need to display the results. We can do so by

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

Thus it will display the IP Address & the Country of the Visitor. If the Country of the Visitor is not listed in the database, it will display the country as " unlisted".

Now, time for the full code in one piece.....

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.

“The greatest performance improvement of all is when a system goes from not-working to working.” - John Ousterhout