許多朋友可能都想做一個IP查詢,這段代碼還是相對簡單的,首先,你要有一個IP地址數據庫,在我的網站上肯定可以下載到,下載地址:http://www.brsky.net/data/IPaddress.mdb,當然我不保證這是最新的,下面我給出一段代碼用來查詢IP地址:
本頁演示地址:http://www.brsky.net/sip.asp
<%
"連接數據庫
dim conn,connstr,db
db="data/ipaddress.mdb"
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(""&db&"")
conn.Open connstr
%>
下面部分為一個提交表單,用于輸入IP數據:這個數據是提交本頁的
<form id="form1" name="form1" method="post" action="">
<label>
請輸入要查詢的IP
<input name="ip" type="text" id="ip" />
</label>
<label>
<input type="submit" name="Submit" value="查詢" />
</label>
</form>
"下面就是查詢和顯示代碼
<%
if not request.form("ip")="" then "如果表單提交了數據就進行處理
sip=trim(request.form("ip"))
cip=split(sip,".")
if ubound(cip)<3 then "如果提交的IP數據不足4位,就補齊
redim Preserve cip(3) "重新定義數組,并保留原來的值
for i=3 to ubound(cip) step -1
cip(i)=0
next
end if
for i=0 to 3 "這里分別檢查數組元素是否為數值字符,如果不是,則可以用IP規則的最大值和最小值分別查,我這里只用最小值
if not IsNumeric(cip(i)) then cip(i)=0
next
ip=256*256*256*cip(0) 256*256*cip(1) 256*cip(2) cip(3)-1
dim rs,sql,Country,City
Set rs=Server.CreateObject("ADODB.Recordset")
sql="select * from Address where IP1<="&ip&" and IP2>="&ip
rs.open sql,conn,1,1
if rs.eof then
City="未知"
Country="未知"
else
City=rs("city")
Country=rs("country")
end if
rs.close
set rs=nothing
response.write "你要查詢的IP是"&sip&",來自"&country&city
end if
%>
新聞熱點
疑難解答