首页 > 开发 > Asp > 正文

纯ASP(VBscript)写的全球IP地址搜

2020-02-03 16:40:11
字体:
来源:转载
供稿:网友
<%@language="vbscript" codepage="936"%>
<html>
<head>
<title>untitled document</title>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
</head>
<body>
<%
dim finishgetip,showip,allip
'////////////////////////////////////////////////////////////////////////////////////////
'程序还不是很精简,以后再修改
'本程序所用的数据库为-- “冯志宏”-- 所写的--“追捕”--软件中所带ip数据库和
' “国华软件 guohua soft”的作者 --“冯国华”—所写的“全球ip地址分配表.chm”合二为一得到的
'感谢“冯志宏”和“冯国华”提供的数据
'数据库中还有不少的重复ip地址,希望有心人能将其删除,减小数据库
'我的程序写的还很笨拙,希望大家能多提意见,多多交流,谢谢!
'////////////////////////////////////////////////////////////////////////////////////////
'解决思路:
'取得的客户端ip一般是202.11.25.1这种,而数据库中的ip格式为202.011.025.001,这就需要将取得的
'客户端ip转换为与数据库中ip一样的格式
'因为目前我们所用的ip是分为4段,每段3位,中间以“.”分隔
'所以我的思路是将客户端ip以“.”符号分割为4段,即202/11/25/1
'然后再分别核对每一段,如果是3位,则不变;如不足3位,为2位,该段前补1个0,为1,同理,则补2个0
'得到格式化后的ip后,去掉ip的最后一段,即取包括“.”的前11位,与数据库中的startip字段的前11位相比较,查找相同的值
'因为从数据库中可以看到,startip和endip的前三段都是一样的,而最后一段不过是内部子网地址,可以去掉
'所以只要取startip或endip的任意一个字段的前11位与客户端ip的前11位相比较就可以查到正确的所在地
'/////////////////////////////////////////////////////////////////////////////////////////
function checkip_trueip()
'取客户端真实ip
getclientip = request.servervariables("http_x_forwarded_for") '如果客户端用了代理服务器,则用request.servervariables("remote_addr")方法只能得到空值,则应该用servervariables("http_x_forwarded_for")方法
if getclientip = "" then
getclientip = request.servervariables("remote_addr")'如果客户端没用代理,则request.servervariables("http_x_forwarded_for")得到是空值,应该用request.servervariables("remote_addr")方法
end if
checkip_trueip = getclientip
end function
'/////////////////////////////////////////////////////////////////////////////
function getaccessrecordset(db,sql,mark,read)'取得recordset对象
set conn=getaccessconn(db)'输入参数为db-数据库的相对路径,sql-sql语句,mark,read为数据库读取方式,1,1为只读,1,3为读写
'constr="provider=microsoft.jet.oledb.4.0;"&"data source="&server.mappath(db)
' conn.open constr
set getaccessrecordset=server.createobject("adodb.recordset")
getaccessrecordset.open sql,conn,mark,read
end function
'//////////////////////////////////////////////////////////////////////////
function getaccessconn(db)'取得connection对象
set getaccessconn=server.createobject("adodb.connection")
'constr="driver={microsoft access driver (*.mdb)};dbq="&server.mappath("allcon/#bbsall.mdb")
constr="provider=microsoft.jet.oledb.4.0;"&"data source="&server.mappath(db)
getaccessconn.open constr
end function
'/////////////////////////////////////////////////////////////////////////
dim getip
'getip=(trim(request.servervariables("remote_addr")))'从客户端获取ip
'getip=(trim(request.querystring("comes"))) '自己输入ip测试
'response.write(getip&"<br>")
'////////////////////////////////////////////////////////////////////////
function checkip_locations(checkstring) '返回ip中分隔字符的位置函数
checkip_locations=instr(checkstring,".") '将位置的值赋予给函数
end function
'///////////////////////////////////////////////////////////////////////
'以下函数为分割ip,取得每次分割后“.”符号右边的ip剩余的字符串
function checkip_left(checkstring)
locations_left=checkip_locations(checkstring) '得到在ip剩余的字符串中“.”第一次出现的位置
iplength_left=len(checkstring) '取得ip剩余的字符串的长度
divide_locations_left=iplength_left-locations_left '取得在ip剩余的字符串中“.”第一次出现的位置,从右往左数是多少位
ipstr_left=right(checkstring,divide_locations_left) '取得本次分割后,“.”符号右边的ip剩余的字符串
checkip_left=ipstr_left '将上面得到的字符串赋给函数
end function
'///////////////////////////////////////////////////////////////////////
'以下函数为分割ip,取得每次分割后“.”符号左边的ip字符串,即将ip分为四段,每一段的字符串

function checkip_right(checkstring)
locations_right=checkip_locations(checkstring) '取得在ip中“.”第一次出现的位置
iplength_right=len(checkstring) '取得ip字符串长度
divide_locations_right=iplength_right-locations_right '取得在ip剩余的字符串中“.”第一次出现的位置,从右往左数是多少位
ipstr11=trim(replace(left(checkstring,locations_right),".","")) '将得到的“.”左边的字符串去掉"."符号
'如果ip分为4段后每一段不足3位则补0
if len(ipstr11)="2" then ipstr11="0"&ipstr11
if len(ipstr11)="3" then ipstr11=ipstr11
if len(ipstr11)="1" then ipstr11="00"&ipstr11
checkip_right=ipstr11 '得到“.”符号之前的字符串,即本次分割后得到的ip分割为四段后其中的一段
end functi

国内最大的酷站演示中心!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表