首页 > 编程 > ASP > 正文

一段查询IP所在地的代码

2024-05-04 11:06:02
字体:
来源:转载
供稿:网友

许多朋友可能都想做一个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
%>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表