【www.gdgbn.com--安卓教程】


<%
Dim IP_address,IP_rs,CIP,IP_conn,IP_connstr,IP_province,IP_city,rs,IP_py
Const DataType=0              "0为Access数据库,1为SQL Server数据库
Const MdbPath="/ip/IPAddress.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+10
 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
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 area from ip where "&CIP&">=ip1 and "&CIP&"<=ip2 order by id desc")
  If IP_rs.Eof Then
   IP_province="中国"
   IP_city="北京"
  Else
      if instr(ip_rs("area")," ")>0 then
      ip_a=split(ip_rs("area")," ")
   if ubound(ip_a)>=2 then
   IP_province=ip_a(0)
   IP_city=ip_a(1)
   else
   IP_province=ip_rs("area")
   IP_city=""
   end if
   else
   IP_province=ip_rs("area")
   IP_city=""
   end if
  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
%>

本文来源:http://www.gdgbn.com/shoujikaifa/19539/