石家庄网站建设公司,网站优化,400电话办理,企业邮箱——明尚互联,石家庄做网站首选

石家庄网站建设公司,网站优化,400电话办理,企业邮箱-明尚互联 石家庄做网站首选

网站建设服务热线
因为专业,所以与众不同

纯真IP数据库TQQWry的ASP读取源码(UTF-8编码)


石家庄明尚互联网络公司 发布于:2011-07-04 13:44:27  点击:3014  字体:

  前些天我遇到了这样的难题,在网上找的ASP读取纯真IP数据库TQQWry的源码是基于GB2312的。用在我的UTF-8的网站上,显示出的地区是乱码。后来经多次研究验证,终于写出了基于UTF-8的ASP读取TQQWry的源码,为此我还有点高兴不以呢,嘿嘿。现在给大家分享一下,相信有很多朋友需要!

以下是引用片段:

' ============================================
' 返回IP地区信息
' ============================================
Function Look_Ip(IP)
 Dim Wry, IPType, QQWryVersion, IpCounter
 ' 设置类对象
 Set Wry = New TQQWry
 ' 开始搜索,并返回搜索结果
 ' 您可以根据 QQWry(IP) 返回值来判断该IP地址在数据库中是否存在,如果不存在可以执行其他的一些操作
 ' 比如您自建一个数据库作为追捕等,这里我就不详细说明了
 IPType = Wry.QQWry(IP)
 ' Country:国家地区字段
 ' LocalStr:省市及其他信息字段
 Look_Ip = Wry.Country & " " & Wry.LocalStr
End Function

' ============================================
' 返回QQWry信息
' ============================================
Function WryInfo()
 Dim Wry, IPType, QQWry(1)
 ' 设置类对象
 Set Wry = New TQQWry
 IPType = Wry.QQWry("255.255.255.255")
 ' 读取数据库版本信息
 QQWry(0) = Wry.Country & " " & Wry.LocalStr
 ' 读取数据库IP地址数目
 QQWry(1) = Wry.RecordCount + 1
 WryInfo = QQWry
End Function

' ============================================
' IP物理定位搜索类
' ============================================
Class TQQWry
 ' ============================================
 ' 变量声名
 ' ============================================
 Dim Country, LocalStr, Buf, OffSet
 Private StartIP, EndIP, CountryFlag
 Public QQWryFile
 Public FirstStartIP, LastStartIP, RecordCount
 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") 'QQ IP库路径,要转换成物理路径
 End Sub
 
 ' ============================================
 ' IP地址转换成整数 ip
  ' ============================================
 Function IPToInt(IP)
  If Instr(IP,":")>0 Then IP="127.0.0.1" ‘当IP地址是::1这样的地址时返回本机地址
  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
  IPToInt = (CInt(IPArray(0))*256*256*256) + (CInt(IPArray(1))*256*256) + (CInt(IPArray(2))*256) + CInt(IPArray(3))
 End Function
 
 ' ============================================
 ' 整数逆转IP地址
 ' ============================================
 Function IntToIP(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
  IntToIP = Cstr(p1) & "." & Cstr(p2) & "." & Cstr(p3) & "." & Cstr(p4)
 End Function
 
 ' ============================================
 ' 获取开始IP位置
 ' ============================================
 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
 
 ' ============================================
 ' 获取结束IP位置
 ' ============================================
 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 >= IPToInt("255.255.255.0") And IP <= IPToInt("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
 
 ' ============================================
 ' 获取IP地址标识符
 ' ============================================
 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
 
 ' ============================================
 ' 获取字串信息 (www.viming.com)
 '-----utf-8-----------
 Private Function GetStr()
  dim c
  getstr = ""
  dim objstream
  set objstream = server.createobject("adodb.stream")
  objstream.type = 1
  objstream.mode =3
  objstream.open
  c = stream.read(1)
  do while (ascb(c)<>0 and not stream.eos)
  objstream.write c
  c = stream.read(1)
  loop
  objstream.position = 0
  objstream.type = 2
  objstream.charset = "gb2312"
  getstr = objstream.readtext
  objstream.close
  set objstream = nothing
 End Function
 
 ' ============================================
 ' 核心函数,执行IP搜索
 ' ============================================
 Public Function QQWry(DotIP)
  Dim IP, nRet
  Dim RangB, RangE, RecNo
  
  IP = IPToInt (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)
  ' 在数据库中找不到任何IP地址
  If (RecordCount <= 1) Then
  Country = "未知"
  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

  纯真IP数据库TQQWry的ASP读取源码(UTF-8编码)调用方法:Look_Ip("123.123.123.123")

  以上代码我还写了一点小小的升级,就是当IP地址是::1这样的地址时,返回本机地址,以免发生错误。

https://www.hiheb.cn/
最新案例
建站套餐
明尚最新动态
手机:19033292827
首页  套餐  服务  案例  关于  留言  联系  新闻
返回头部
联系我们
Contact
QQ咨询623900980  QQ咨询81457951
业务咨询:19033292827
地址:河北省石家庄市长安区古城西路汇春博物园
Copyright © 2007-2024 石家庄明尚信息技术有限公司
备案编号:冀ICP备12016215号-2 冀公网安备13010502002383号
业务范围:石家庄网站建设 石家庄网站优化 石家庄网站制作 石家庄做网站 石家庄建网站 石家庄建站 石家庄网页设计