Web Hosting Info

Search:

featured partner

The IP to Country Database

  Forum Topics : Support / ASP code for IP number
Submitted by pbol on Mon, 11/24/2003 - 12:07.
I guess most people managed this simple task but I saw someone asking about it and it's not on the "handbook" pages.

Simple classic ASP code to convert the IP:

Function ConvertIP(sIP)
Dim arr
arr = Split(sIP,".")
ConvertIP = arr(0)*16777216 + arr(1)*65536 + arr(2)*256 + arr(3)
End Function

I guess you could do some error handling as well but if you take the IP from Request.ServerVariables("REMOTE_HOST") it's always the correct format.
Comment viewing options:
Select your preferred way to display the comments and click 'Save settings' to submit your changes.
Correction
Posted by pbol on Mon, 11/24/2003 - 14:01.
Sorry, I mean use the REMOTE_ADDR since the REMOTE_HOST potentially could contain a reverse dns name if it's turned on.

Also, look out for wrapping in that code.
 
Wrong country
Posted by KJK on Tue, 05/25/2004 - 18:16.
Hi:
I am trying to run a query against the ip to country database.

IP is 209.52.137.246
Converted IP is 3509881334

country is UNITED STATES - which is wrong - it should be canada.

I used the convert code off this site.
What am I doing wrong?
Attached is my code.

sIP = request("REMOTE_ADDR")
Function ConvertIP(sIP)
Dim arr
arr = Split(sIP,".")
ConvertIP = arr(0)*16777216 + arr(1)*65536 + arr(2)*256 + arr(3)
End Function
strIP = ConvertIP(sIP)
set myConnection = Server.CreateObject("ADODB.Connection")
myconnection.open Session("dbNet")
sqlString = "SELECT * FROM tblCountryIP WHERE IP_From<='" & strIP & "' AND IP_To>='" & strIP & "'"
set rsList1 = myConnection.execute(sqlString)
strCountry = rsList1("country_name")
Thanks in advance.
Kerstin
 
The problem & solution
Posted by mystiq on Thu, 05/27/2004 - 23:58.
Well, as I see, the problem is in
sIP = request("REMOTE_ADDR")
It should be
sIP = Request.ServerVariables("REMOTE_ADDR")


_____________________________
Mystiq
~~~~~~~~~~~~~~~~~~~~~
A new Face EVERYDAY
 
 
I have the same problem
Posted by meteor73 on Tue, 08/10/2004 - 06:12.
Dear experts,

I come from Hong Kong, and I am using the IP->Country database which dated 29/7/2004. I have set up a testing page to test the code and found that the result is not correct. 1.It is not the right country. 2.It is differ from the result in ip-to-country.webhosting.info's demo. Could anyone help me to fix it?

IP address: 219.76.171.237
Country of origin: Hong Kong
Result generated by testing: United States
Code of testing page:

IPAddress=request.ServerVariables("REMOTE_ADDR")
IPCode=split(IPAddress,".")
for i=0 to ubound(IPCode)
IPFin=IPFin + (IPCode(i)*(255^(3-i)))
next
Dim rsListIP
Dim rsListIP_numRows
Set rsListIP = Server.CreateObject("ADODB.Recordset")
rsListIP.ActiveConnection = MM_OnlineDB_STRING
rsListIP.Source = "SELECT LongCountry FROM IPCountry WHERE Code1<=" & IPFin & " AND Code2>=" & IPFin
rsListIP.CursorType = 0
rsListIP.CursorLocation = 2
rsListIP.LockType = 1
rsListIP.Open()
rsListIP_numRows = 0
if rsListIP.eof=true then
CountryName="Unknown"
else
CountryName=(rsListIP.Fields.Item("LongCountry").Value)
end if
rsListIP.Close()
Set rsListIP = Nothing
response.Write(CountryName & "<br>" & IPAddress & <br>" & IPFin)