asp IP轉城市資料庫代碼下載

來源:互聯網
上載者:User


<%
Dim IP_address,IP_rs,CIP,IP_conn,IP_connstr,IP_province,IP_city
Const DataType=1       '0為Access資料庫,1為SQL Server資料庫
Const MdbPath="IP_Address.mdb"  '如果是SQL Server資料庫,此項留空
ip_address=Request.ServerVariables("REMOTE_ADDR") '擷取用戶端IP地址

Function GetRegion()
 If Request.Cookies("sonrun")("IP_province")&""<>"" And Request.Cookies("sonrun")("IP_city")&""<>"" Then
  IP_province=Request.Cookies("sonrun")("IP_province")
  IP_city=Request.Cookies("sonrun")("IP_city")
 Else
  Call SelectData()
  Response.Cookies("sonrun")("IP_province")=IP_province
  Response.Cookies("sonrun")("IP_city")=IP_city
  Response.Cookies("sonrun").Expires=Date+1
 End If
End Function

Sub IPConn()
 On Error Resume Next
 If DataType=0 Then
  IP_connstr="Provider=Microsoft.Jet.OLEDB.4.0; Data Source="&Server.MapPath(MdbPath)
 Else
  IP_connstr="driver={SQL Server}; server=127.0.0.1;database=IP_Address;uid=sa;pwd="
 End If
 Set IP_conn=Server.CreateObject("Adodb.Connection")
 IP_conn.Open IP_connstr
 If Err.Number<>0 Then
  Err.Clear
  Response.Write("資料庫連接失敗!")
  Response.End()
 End If
End Sub

Function IP2Num(IP) '將IP轉換為數字格式
 Dim IPArr  
 IPArr=Split(IP,".")
 IP2Num=Int(IPArr(0))*256*256*256+Int(IPArr(1))*256*256+Int(IPArr(2))*256+Int(IPArr(3))
End Function

Function SelectData()
 If ip_address<>"" Then
  CIP=IP2Num(ip_address)
  Call IPConn()
  Set IP_rs=IP_conn.Execute("select top 1 city,province from ip_address where "&CIP&">=ip1 and "&CIP&"<=ip2 order by id desc")
  If IP_rs.Eof Then
   IP_province="北京"
   IP_city="北京"
  Else
   IP_province=IP_rs(1)
   IP_city=IP_rs(0)
  End if
  IP_rs.Close
  Set IP_rs=Nothing
  IP_conn.Close
  Set IP_conn=Nothing
 Else
  IP_province="北京"
  IP_city="北京"
 End If
End Function
%>
調用方法
<!--#include File="Function.asp" -->
<%
Call GetRegion()
Response.Write(IP_province&"-"&IP_city)
%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.