欢迎光临
我们一直在努力

ASP编写完整的一个IP所在地搜索类

建站超值云服务器,限时71元/月

<%

server.scripttimeout = &h3c

response.buffer = ("s.f." = "s.f.")

dim ipsearch

建立对象

set ipsearch = new clsipsearch

该句建立sql server的ip地址库的连接,可使用默认连接,但要保证存在wry.mdb

ipsearch.connectionstring = "driver={sql server};server=hostname:uid=sa;pwd=;database=ip"

设置要查询的ip,可用默认值,这里设置的是 127.0.0.1

ipsearch.ipaddress = &h7f & "." & &h00 & "." & &h00 & "." & &h01

取得ip 所在地,反馈值有三个,以逗号分割

格式为:所在国家或地区,当地上网地区,提供正确ip地址信息的用户名

response.write ("所在地:" & ipsearch.getipaddrinfo() & "<br>")

取出ip地址

response.write ("ip:" & ipsearch.ipaddress & "<br>")

将ip地址转换为数值

response.write ("ip转换为数值:" & ipsearch.clongip(ipsearch.ipaddress) & "<br>")

将ip地址转换为数值后还原成ip字符串

response.write ("数值还原成ip:" & ipsearch.cstringip(ipsearch.clongip(ipsearch.ipaddress)) & "<br>")

response.write ("<hr>")

%>

<%

class clsipsearch

##################################################################

声明:本程序采用的数据为网络上著名的ip工具软件《追捕》作者“冯志宏”

先生所精心搜集整理。

《追捕》数据库的转换方法:

修改wry.dll 文件后缀名称为 wry.dbf

方法一:

启动access 数据,选择打开数据库,选择打开的文件类型为“dbase 5 (*.dbf)”

打开wry.dbf文件,选择《工具》菜单下的《数据库实用工具》中的《转换数据库》

选择《转换为 access 97 格式(版本可选)》功能,保存文件即可成为mdb格式。

方法二:

使用sql server提供的《导入和导出数据》向导。

方法简要说明:在odbc 控制面板中设置指向wry.dbf的dsn。

使用《导入和导出数据》向导,选择其正确的驱动程序和要导入的库即可。

或者直接导入由方法一生成的mdb文件入库。

方法三:

使用access 打开wry.dbf 文件后将自动通过mdb库引用原库数据。

未安装其他数据库平台,其他方法欠考虑。

###################### 类说明 ####################################

# ip 所在地搜索类

# connectionstring 为数据库连接声明,默认声明同级目录的wry.mdb

# ipaddress 请设置为进行搜索的ip 地址,默认取当前访问者ip

# 类建立方法

# dim objval 声明一个变量

# set objval = new clsipsearch 建立类对象

# response.write (objval.ipaddress) 显示当前访问者ip

# ip 搜索类方法列表:

# .clongip 将ip地址转换为长整型的数值

# 参数:asnewip 要转换的ip地址字符串

# .cstringip 将长整型的数值转换为ip

# 参数:annewip 要还原为ip地址的数值

# .getclientip 取访问者的ip

# .getipaddrinfo 得到设置过ipaddress属性的ip所在地

# 属性列表(自动初始化):

# connectionstring ado 访问数据库连接说明

# ipaddress 要操作的ip地址

# 内部错误处理:

# 欠缺,未做,请自行补充。

##################################################################

public connectionstring

public ipaddress

private dbconn 连接对象,模块级声明

────────────────────────────────

类初始化

private sub class_initialize()

这里建立的是通过“数据转换–方法一”生成的mdb 库文件

connectionstring="driver={microsoft access driver (*.mdb)};dbq=" & server.mappath("wry.mdb")

ipaddress = getclientip()

set dbconn = openconnection()

end sub

────────────────────────────────

类注销

private sub class_terminate()

connectionstring = null

ipaddress = null

dbconn.close

set dbconn = nothing

end sub

────────────────────────────────

建立一个连接

private function openconnection()

dim tmpconn

set tmpconn=server.createobject("adodb.connection")

tmpconn.open connectionstring

set openconnection=tmpconn

set tmpconn=nothing

end function

────────────────────────────────

执行一个sql命令,并返回一个数据集对象

private function sqlexecute(strsql)

dim rs

set rs=dbconn.execute(strsql)

set sqlexecute = rs

set rs=nothing

end function

────────────────────────────────

转换一个数值为ip

public function cstringip(byval annewip)

dim lsresults

dim lntemp

dim lnindex

for lnindex = &h03 to &h00 step -&h01

lntemp = int(annewip / (&h100 ^ lnindex))

lsresults = lsresults & lntemp & "."

annewip = annewip – (lntemp * (&h100 ^ lnindex))

next

lsresults = left(lsresults, len(lsresults) – &h01)

cstringip = lsresults

end function

────────────────────────────────

转换一个ip到数值

public function clongip(byval asnewip)

dim lnresults

dim lnindex

dim lnipary

lnipary = split(asnewip, ".", &h04)

for lnindex = &h00 to &h03

if not lnindex = &h03 then

lnipary(lnindex) = lnipary(lnindex) * (&h100 ^ (&h03 – lnindex))

end if

lnresults = lnresults + lnipary(lnindex)

next

clongip = lnresults

end function

────────────────────────────────

取client ip

public function getclientip()

dim uipaddr

本函数参考webcn.net/asphouse 文献<取真实的客户ip>

uipaddr = request.servervariables("http_x_forwarded_for")

if uipaddr = "" then uipaddr = request.servervariables("remote_addr")

getclientip = uipaddr

uipaddr = ""

end function

────────────────────────────────

读取ip所在地的信息

public function getipaddrinfo()

dim tmpipaddr

dim ipaddrval

dim ic,charspace

dim outtime

outtime = timer

charspace = ""

ipaddrval = ipaddress

将ip字符串劈开成数组好进行处理

tmpipaddr = split(ipaddrval,".",-1,1)

for ic = &h00 to ubound(tmpipaddr)

补位操作,保证每间隔满足3个字符

select case len(tmpipaddr(ic))

case &h01 :charspace = "00"

case &h02 :charspace = "0"

case else :charspace = ""

end select

tmpipaddr(ic) = charspace & tmpipaddr(ic)

next

ipaddrval = tmpipaddr(&h00) & "." & tmpipaddr(&h01) & "." & tmpipaddr(&h02) & "." & tmpipaddr(&h03)

以下为查询,ip地址库基于《追捕》的ip数据库,感谢"冯志宏"先生的贡献

库结构如下:

create table [dbo].[wry] (

[startip] [nvarchar] (17) collate chinese_prc_ci_as null , –起始ip段

[endip] [nvarchar] (17) collate chinese_prc_ci_as null , –终止ip段

[country] [nvarchar] (16) collate chinese_prc_ci_as null , –国家或者地区

[local] [nvarchar] (54) collate chinese_prc_ci_as null , –本地地址

[thank] [nvarchar] (23) collate chinese_prc_ci_as null –感谢修正ip地址用户姓名

) on [primary]

经过分析库的数据存放结构,总结出准确的查询方法,具体看下面的查询过程

假设查询的ip为 127.0.0.1,对照库数据将其修改为127.000.000.001

然后各搜索两次startip字段和endip字段,分别分割字符长度为11,7

这样在前两次搜索startip 未找到正确数据,将对比endip字段。

如果按如下顺序查找,任何一条语句满足将停止查询

select * from wry where left(ltrim(rtrim(startip)),11) = 127.000.000

select * from wry where left(ltrim(rtrim(startip)),7) = 127.000

select * from wry where left(ltrim(rtrim(endip)),11) = 127.000.000

select * from wry where left(ltrim(rtrim(endip)),7) = 127.000

本原意打算采用一次查询得到结果,但本人sql 查询表达式的功力不够,会发生

查出有两条记录的数据,考虑到这种小范围的数据查询速度很快所以采用这种比较

直观的查询完成。关注朋友修正,感谢。

for ic = &h01 to &h04

select case ic

case &h01:

查询startip 字段,取位 11

charspace = getdbipinfo(ipaddrval,&h0b,"startip")

if len(charspace)>&h00 then exit for

case &h02:

查询startip 字段,取位 7

charspace = getdbipinfo(ipaddrval,&h07,"startip")

if len(charspace)>&h00 then exit for

case &h03:

查询endip 字段,取位 11

charspace = getdbipinfo(ipaddrval,&h0b,"endip")

if len(charspace)>&h00 then exit for

case &h04:

查询endip 字段,取位 7

charspace = getdbipinfo(ipaddrval,&h07,"endip")

if len(charspace)>&h00 then exit for

end select

next

if len(charspace)=&h00 then

getipaddrinfo =false

else

getipaddrinfo = charspace

end if

end function

────────────────────────────────

返回数据查询的字符串

private function getdbipinfo(byval ipaddr,byval iplength,byval findmethod)

dim openipsearch

dim strsql

dim result

strsql = "select * from wry where left(ltrim(rtrim(" & findmethod & "))," & iplength & ") = " & left(trim(ipaddr),iplength) & ""

set openipsearch = sqlexecute(strsql)

if not openipsearch.eof then

result = openipsearch(nulltospace("country")) & "," & openipsearch(nulltospace("local")) & "," & openipsearch(nulltospace("thank"))

else

result = null

end if

openipsearch.close

set openipsearch = nothing

getdbipinfo = result

end function

────────────────────────────────

将数据库空记录转换为空字符

private function nulltospace(byval rsstr)

if isnull(rsstr) then

nulltospace = ""

else

nulltospace = trim(rsstr)

end if

end function

end class

%>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » ASP编写完整的一个IP所在地搜索类
分享到: 更多 (0)

相关推荐

  • 暂无文章