找回密码
 加入
搜索

[代码] IPaddress通过ip查询地理位置

[复制链接]
发表于 2007-3-1 06:07:55 | 显示全部楼层 |阅读模式
  1. <%
  2. Function getIP()
  3.     Dim strIPAddr
  4.     If Request.ServerVariables("HTTP_X_FORWARDED_FOR") = "" OR InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), "unknown") > 0 Then
  5.         strIPAddr = Request.ServerVariables("REMOTE_ADDR")
  6.     ElseIf InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",") > 0 Then
  7.         strIPAddr = Mid(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), 1, InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ",")-1)
  8.     ElseIf InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ";") > 0 Then
  9.         strIPAddr = Mid(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), 1, InStr(Request.ServerVariables("HTTP_X_FORWARDED_FOR"), ";")-1)
  10.     Else
  11.         strIPAddr = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
  12.     End If
  13.     getIP = Trim(Mid(strIPAddr, 1, 30))
  14. End Function
  15. &#39; ============================================
  16. &#39; 返回IP信息 JS调用
  17. &#39; ============================================
  18. Function Disp_IPAddressData(IP, sType)
  19.         Dim Wry, IPType
  20.         Set Wry = New TQQWry
  21.         IPType = Wry.QQWry(IP)
  22.         
  23.         Select Case sType
  24.                 Case 1 Disp_IPAddressData = IP
  25.                 Case 2 Disp_IPAddressData = Wry.Country
  26.                 Case 3 Disp_IPAddressData = Wry.LocalStr
  27.                 &#39;Case Else Disp_IPAddressData = Wry.Country & "" & Wry.LocalStr
  28.                 Case Else Disp_IPAddressData = Wry.Country
  29.         End Select
  30. End Function
  31. &#39; ============================================
  32. &#39; 返回QQWry信息
  33. &#39; ============================================
  34. Function WryInfo()
  35.         Dim Wry, IPType, QQWry_tem(0), QQWry_tem1(1)
  36.         Set Wry = New TQQWry
  37.         IPType = Wry.QQWry("255.255.255.254")
  38.         QQWry_tem(0) = Wry.Country & " " & Wry.LocalStr
  39.         QQWry_tem1(1) = Wry.RecordCount + 1
  40.         WryInfo = QQWry_tem(0)& " " & QQWry_tem1(1)
  41. End Function

  42. Class TQQWry
  43.         &#39; ============================================
  44.         &#39; 变量声名
  45.         &#39; ============================================
  46.         Dim Country, LocalStr, Buf, OffSet
  47.         Private StartIP, EndIP, CountryFlag
  48.         Public QQWryFile
  49.         Public FirstStartIP, LastStartIP, RecordCount
  50.         Private Stream, EndIPOff
  51.         &#39; ============================================
  52.         &#39; 类模块初始化
  53.         &#39; ============================================
  54.         Private Sub Class_Initialize
  55.                 Country                = ""
  56.                 LocalStr                = ""
  57.                 StartIP                = 0
  58.                 EndIP                        = 0
  59.                 CountryFlag        = 0
  60.                 FirstStartIP        = 0
  61.                 LastStartIP        = 0
  62.                 EndIPOff                = 0
  63.                 QQWryFile = Server.MapPath("../inc/QQWry.Dat")  &#39;注意修改路径
  64.         End Sub



  65.         Function IPToInt(IP)
  66.                 Dim IPArray, i
  67.                 IPArray = Split(IP, ".", -1)
  68.                 FOr i = 0 to 3
  69.                         If Not IsNumeric(IPArray(i)) Then IPArray(i) = 0
  70.                         If CInt(IPArray(i)) < 0 Then IPArray(i) = Abs(CInt(IPArray(i)))
  71.                         If CInt(IPArray(i)) > 255 Then IPArray(i) = 255
  72.                 Next
  73.                 IPToInt = (CInt(IPArray(0))*256*256*256) + (CInt(IPArray(1))*256*256) + (CInt(IPArray(2))*256) + CInt(IPArray(3))
  74.         End Function
  75.         &#39; ============================================
  76.         &#39; 整数逆转IP地址
  77.         &#39; ============================================
  78.         Function IntToIP(IntValue)
  79.                 p4 = IntValue - Fix(IntValue/256)*256
  80.                 IntValue = (IntValue-p4)/256
  81.                 p3 = IntValue - Fix(IntValue/256)*256
  82.                 IntValue = (IntValue-p3)/256
  83.                 p2 = IntValue - Fix(IntValue/256)*256
  84.                 IntValue = (IntValue - p2)/256
  85.                 p1 = IntValue
  86.                 IntToIP = Cstr(p1) & "." & Cstr(p2) & "." & Cstr(p3) & "." & Cstr(p4)
  87.         End Function
  88.         &#39; ============================================
  89.         &#39; 获取开始IP位置
  90.         &#39; ============================================
  91.         Private Function GetStartIP(RecNo)
  92.                 OffSet = FirstStartIP + RecNo * 7
  93.                 Stream.Position = OffSet
  94.                 Buf = Stream.Read(7)
  95.                
  96.                 EndIPOff = AscB(MidB(Buf, 5, 1)) + (AscB(MidB(Buf, 6, 1))*256) + (AscB(MidB(Buf, 7, 1))*256*256)
  97.                 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)
  98.                 GetStartIP = StartIP
  99.         End Function
  100.         &#39; ============================================
  101.         &#39; 获取结束IP位置
  102.         &#39; ============================================
  103.         Private Function GetEndIP()
  104.                 Stream.Position = EndIPOff
  105.                 Buf = Stream.Read(5)
  106.                 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)
  107.                 CountryFlag = AscB(MidB(Buf, 5, 1))
  108.                 GetEndIP = EndIP
  109.         End Function


  110.         Private Sub GetCountry(IP)
  111.                 If (CountryFlag = 1 Or CountryFlag = 2) Then
  112.                         Country = GetFlagStr(EndIPOff + 4)
  113.                         If CountryFlag = 1 Then
  114.                                 LocalStr = GetFlagStr(Stream.Position)
  115.                               
  116.                                 If IP >= IPToInt("255.255.255.0") And IP <= IPToInt("255.255.255.255") Then
  117.                                         LocalStr = GetFlagStr(EndIPOff + 21)
  118.                                         Country = GetFlagStr(EndIPOff + 12)
  119.                                 End If
  120.                         Else
  121.                                 LocalStr = GetFlagStr(EndIPOff + 8)
  122.                         End If
  123.                 Else
  124.                         Country = GetFlagStr(EndIPOff + 4)
  125.                         LocalStr = GetFlagStr(Stream.Position)
  126.                 End If
  127.             
  128.                 Country = Trim(Country)
  129.                 LocalStr = Trim(LocalStr)
  130.                 If InStr(Country, "CZ88.NET") Then Country = ""
  131.                 If InStr(LocalStr, "CZ88.NET") Then LocalStr = ""
  132.         End Sub


  133.         Private Function GetFlagStr(OffSet)
  134.                 Dim Flag
  135.                 Flag = 0
  136.                 Do While (True)
  137.                         Stream.Position = OffSet
  138.                         Flag = AscB(Stream.Read(1))
  139.                         If(Flag = 1 Or Flag = 2 ) Then
  140.                                 Buf = Stream.Read(3)
  141.                                 If (Flag = 2 ) Then
  142.                                         CountryFlag = 2
  143.                                         EndIPOff = OffSet - 4
  144.                                 End If
  145.                                 OffSet = AscB(MidB(Buf, 1, 1)) + (AscB(MidB(Buf, 2, 1))*256) + (AscB(MidB(Buf, 3, 1))*256*256)
  146.                         Else
  147.                                 Exit Do
  148.                         End If
  149.                 Loop
  150.                
  151.                 If (OffSet < 12 ) Then
  152.                         GetFlagStr = ""
  153.                 Else
  154.                         Stream.Position = OffSet
  155.                         GetFlagStr = GetStr()
  156.                 End If
  157.         End Function


  158.         Private Function GetStr()
  159.                 Dim c
  160.                 GetStr = ""
  161.                 Do While (True)
  162.                         c = AscB(Stream.Read(1))
  163.                         If (c = 0) Then Exit Do
  164.                         
  165.                         
  166.                         If c > 127 Then
  167.                                 If Stream.EOS Then Exit Do
  168.                                 GetStr = GetStr & Chr(AscW(ChrB(AscB(Stream.Read(1))) & ChrB(C)))
  169.                         Else
  170.                                 GetStr = GetStr & Chr(c)
  171.                         End If
  172.                 Loop
  173.         End Function



  174.         Public Function QQWry(DotIP)
  175.                 Dim IP, nRet
  176.                 Dim RangB, RangE, RecNo
  177.                
  178.                 IP = IPToInt (DotIP)
  179.                
  180.                 Set Stream = CreateObject("ADodb.Stream")
  181.                 Stream.Mode = 3
  182.                 Stream.Type = 1
  183.                 Stream.Open
  184.                 Stream.LoadFromFile QQWryFile
  185.                 Stream.Position = 0
  186.                 Buf = Stream.Read(8)
  187.                
  188.                 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)
  189.                 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)
  190.                 RecordCount = Int((LastStartIP - FirstStartIP)/7)
  191.                
  192.                 If (RecordCount <= 1) Then
  193.                         Country = "未知"
  194.                         QQWry = 2
  195.                         Exit Function
  196.                 End If
  197.                
  198.                 RangB = 0
  199.                 RangE = RecordCount
  200.                
  201.                 Do While (RangB < (RangE - 1))
  202.                         RecNo = Int((RangB + RangE)/2)
  203.                         Call GetStartIP (RecNo)
  204.                         If (IP = StartIP) Then
  205.                                 RangB = RecNo
  206.                                 Exit Do
  207.                         End If
  208.                         If (IP > StartIP) Then
  209.                                 RangB = RecNo
  210.                         Else
  211.                                 RangE = RecNo
  212.                         End If
  213.                 Loop
  214.                
  215.                 Call GetStartIP(RangB)
  216.                 Call GetEndIP()

  217.                 If (StartIP <= IP) And ( EndIP >= IP) Then
  218.                      
  219.                         nRet = 0
  220.                 Else
  221.                      
  222.                         nRet = 3
  223.                 End If
  224.                 Call GetCountry(IP)

  225.                 QQWry = nRet
  226.         End Function



  227.         Private Sub Class_Terminate
  228.                 On ErrOr Resume Next
  229.                 Stream.Close
  230.                 If Err Then Err.Clear
  231.                 Set Stream = Nothing
  232.         End Sub
  233. End Class

  234. %>
  235. 调用的时候<%=Disp_IPAddressData(getip(),2)%>
复制代码

Archiver|手机版|小黑屋|乱弹琴|重庆网址之家|多来米发 |

GMT+8, 2026-10-13 09:59 PM

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表