ip能显示在asp网页表格但是地理位置要怎么做才能显示出来
不推荐调用第三方接口,不稳定,而且查询效率太低。
几乎所有的网站程序显示IP地理位置都是通过本地的IP地址数据库实现的,而使用的IP地址数据库多数是纯真网络的,你可以在http://update.cz88.net/soft/setup.zip下载到
下面的函数是用来调用纯真IP数据库查询IP归属地的
首先,将你下载的setup.zip解压缩,执行解压出来的setup.exe安装
安装后启动“纯真IP地址数据库”,可以查看到IP地址的更新时间,通过点击“在线升级”可以更新IP数据库(因为有时候一些IP的分配会改变,所以IP数据库长年不更新会不准确)
确保你的IP数据是最新的后,找到“纯真IP地址数据库”的安装文件夹(既然楼主是做程序开发的,应该会找程序的安装目录,不多解释,不明白可以追问),将安装文件夹下的qqwry.dat拷贝到你的网站目录,这个就是IP数据库了。
下面的函数是查询qqwry.dat用的,注意Server.MapPath("QQWry.dat")处的QQWry.dat路径要正确。
调用方法:
GetIpInfo("IP地址")
如:
IPLocation=GetIpInfo("192.168.1.1")
response.write IPLocation
<%
'=================获取地理位置函数
Function GetIpInfo(IP)
Dim Wry,IPType
Set Wry=New TQQWry
IPType=Wry.QQWry(IP)
GetIpInfo=Wry.Country&Wry.LocalStr
Set Wry = Nothing
End Function
'=================
'=================IP地理检索类对象
Class TQQWry
Dim Country,LocalStr,Buf,OffSet
Private StartIP,EndIP,CountryFlag
Public FirstStartIP,LastStartIP,RecordCount,QQWryFile
Private Stream,EndIPOff
Private Sub Class_Initialize
Country=""
LocalStr=""
StartIP=0
EndIP=0
CountryFlag=0
FirstStartIP=0
LastStartIP=0
EndIPOff=0
QQWryFile=Server.MapPath("QQWry.dat")
End Sub
Function IP2Int(IP)
Dim IPArray,i
IPArray=Split(IP,".",-1)
FOr i=0 to 3
If Not IsNumeric(IPArray(i)) Then IPArray(i)=0
If CInt(IPArray(i))<0 Then IPArray(i)=Abs(CInt(IPArray(i)))
If CInt(IPArray(i))>255 Then IPArray(i)=255
Next
IP2Int=(CInt(IPArray(0))*256*256*256)+(CInt(IPArray(1))*256*256)+(CInt(IPArray(2))*256)+CInt(IPArray(3))'-1
End Function
Function Int2IP(IntValue)
p4=IntValue-Fix(IntValue/256)*256
IntValue=(IntValue-p4)/256
p3=IntValue-Fix(IntValue/256)*256
IntValue=(IntValue-p3)/256
p2=IntValue-Fix(IntValue/256)*256
IntValue=(IntValue-p2)/256
p1=IntValue
Int2IP=Cstr(p1)&"."&Cstr(p2)&"."&Cstr(p3)&"."&Cstr(p4)
End Function
Private Function GetStartIP(RecNo)
OffSet=FirstStartIP+RecNo * 7
Stream.Position=OffSet
Buf=Stream.Read(7)
EndIPOff=AscB(MidB(Buf,5,1))+(AscB(MidB(Buf,6,1))*256)+(AscB(MidB(Buf,7,1))*256*256)
StartIP=AscB(MidB(Buf,1,1))+(AscB(MidB(Buf,2,1))*256)+(AscB(MidB(Buf,3,1))*256*256)+(AscB(MidB(Buf,4,1))*256*256*256)
GetStartIP=StartIP
End Function
Private Function GetEndIP()
Stream.Position=EndIPOff
Buf=Stream.Read(5)
EndIP=AscB(MidB(Buf,1,1))+(AscB(MidB(Buf,2,1))*256)+(AscB(MidB(Buf,3,1))*256*256)+(AscB(MidB(Buf,4,1))*256*256*256)
CountryFlag=AscB(MidB(Buf,5,1))
GetEndIP=EndIP
End Function
Private Sub GetCountry(IP)
If (CountryFlag=1 Or CountryFlag=2) Then
Country=GetFlagStr(EndIPOff+4)
If CountryFlag=1 Then
LocalStr=GetFlagStr(Stream.Position)
If IP>= IP2Int("255.255.255.0") And IP<=IP2Int("255.255.255.255") Then
LocalStr=GetFlagStr(EndIPOff+21)
Country=GetFlagStr(EndIPOff+12)
End If
Else
LocalStr=GetFlagStr(EndIPOff+8)
End If
Else
Country=GetFlagStr(EndIPOff+4)
LocalStr=GetFlagStr(Stream.Position)
End If
Country=Trim(Country)
LocalStr=Trim(LocalStr)
If InStr(Country,"CZ88.NET") Then Country=""
If InStr(LocalStr,"CZ88.NET") Then LocalStr=""
End Sub
Private Function GetFlagStr(OffSet)
Dim Flag
Flag=0
Do While (True)
Stream.Position=OffSet
Flag=AscB(Stream.Read(1))
If(Flag=1 Or Flag=2 ) Then
Buf=Stream.Read(3)
If (Flag=2 ) Then
CountryFlag=2
EndIPOff=OffSet-4
End If
OffSet=AscB(MidB(Buf,1,1))+(AscB(MidB(Buf,2,1))*256)+(AscB(MidB(Buf,3,1))*256*256)
Else
Exit Do
End If
Loop
If (OffSet<12 ) Then
GetFlagStr=""
Else
Stream.Position=OffSet
GetFlagStr=GetStr()
End If
End Function
Private Function GetStr()
Dim c
GetStr=""
Do While (True)
c=AscB(Stream.Read(1))
If (c=0) Then Exit Do
If c>127 Then
If Stream.EOS Then Exit Do
GetStr=GetStr&Chr(AscW(ChrB(AscB(Stream.Read(1)))&ChrB(C)))
Else
GetStr=GetStr&Chr(c)
End If
Loop
End Function
Public Function QQWry(DotIP)
Dim IP,nRet
Dim RangB,RangE,RecNo
IP=IP2Int(DotIP)
Set Stream=CreateObject("ADodb.Stream")
Stream.Mode=3
Stream.Type=1
Stream.Open
Stream.LoadFromFile QQWryFile
Stream.Position=0
Buf=Stream.Read(8)
FirstStartIP=AscB(MidB(Buf,1,1))+(AscB(MidB(Buf,2,1))*256)+(AscB(MidB(Buf,3,1))*256*256)+(AscB(MidB(Buf,4,1))*256*256*256)
LastStartIP=AscB(MidB(Buf,5,1))+(AscB(MidB(Buf,6,1))*256)+(AscB(MidB(Buf,7,1))*256*256)+(AscB(MidB(Buf,8,1))*256*256*256)
RecordCount=Int((LastStartIP-FirstStartIP)/7)
If (RecordCount<=1) Then
Country="Unknow"
QQWry=2
Exit Function
End If
RangB=0
RangE=RecordCount
Do While (RangB<(RangE-1))
RecNo=Int((RangB+RangE)/2)
Call GetStartIP (RecNo)
If (IP=StartIP) Then
RangB=RecNo
Exit Do
End If
If (IP>StartIP) Then
RangB=RecNo
Else
RangE=RecNo
End If
Loop
Call GetStartIP(RangB)
Call GetEndIP()
If (StartIP<=IP) And ( EndIP>=IP) Then
nRet=0
Else
nRet=3
End If
Call GetCountry(IP)
QQWry=nRet
End Function
Private Sub Class_Terminate()
On ErrOr Resume Next
Stream.Close
If Err Then Err.Clear
Set Stream=Nothing
End Sub
End Class
'=================
%>
希望能够解决您的问题,如有进一步疑问欢迎追加。
2014-09-26