ASP利用純真數(shù)據(jù)庫(kù)查詢IP地理位置
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
asp查詢純真ip的類,請(qǐng)保存為cz.asp[br]<% [br]class tqqwry [br]dim country,localstr,buf,offset [br]private startip,endip,countryflag [br]public firststartip,laststartip,recordcount,qqwryfile [br]private stream,endipoff [br][br]private sub class_initialize [br]country="" [br]localstr="" [br]startip=0 [br]endip=0 [br]countryflag=0 [br]firststartip=0 [br]laststartip=0 [br]endipoff=0 [br]qqwryfile=server.mappath("cz.dat") [br]end sub [br][br]public sub setpath(p) [br]qqwryfile = server.mappath(p) [br]end sub [br][br]function ip2int(ip) [br]dim iparray,i [br]iparray=split(ip,".",-1) [br]for i=0 to 3 [br]if not isnumeric(iparray(i)) then iparray(i)=0 [br]if cint(iparray(i))<0 then iparray(i)=abs(cint(iparray(i))) [br]if cint(iparray(i))>255 then iparray(i)=255 [br]next [br]ip2int=(cint(iparray(0))*256*256*256)+(cint(iparray(1))*256*256)+(cint(iparray(2))*256)+cint(iparray(3))'-1 [br]end function [br][br]function int2ip(intvalue) [br]p4=intvalue-fix(intvalue/256)*256 [br]intvalue=(intvalue-p4)/256 [br]p3=intvalue-fix(intvalue/256)*256 [br]intvalue=(intvalue-p3)/256 [br]p2=intvalue-fix(intvalue/256)*256 [br]intvalue=(intvalue-p2)/256 [br]p1=intvalue [br]int2ip=cstr(p1)&"."&cstr(p2)&"."&cstr(p3)&"."&cstr(p4) [br]end function [br][br]private function getstartip(recno) [br]offset=firststartip+recno * 7 [br]stream.position=offset [br]buf=stream.read(7) [br][br]endipoff=ascb(midb(buf,5,1))+(ascb(midb(buf,6,1))*256)+(ascb(midb(buf,7,1))*256*256) [br]startip=ascb(midb(buf,1,1))+(ascb(midb(buf,2,1))*256)+(ascb(midb(buf,3,1))*256*256)+(ascb(midb(buf,4,1))*256*256*256) [br]getstartip=startip [br]end function [br][br]private function getendip() [br]stream.position=endipoff [br]buf=stream.read(5) [br]endip=ascb(midb(buf,1,1))+(ascb(midb(buf,2,1))*256)+(ascb(midb(buf,3,1))*256*256)+(ascb(midb(buf,4,1))*256*256*256) [br]countryflag=ascb(midb(buf,5,1)) [br]getendip=endip [br]end function [br][br]private sub getcountry(ip) [br]if (countryflag=1 or countryflag=2) then [br]country=getflagstr(endipoff+4) [br]if countryflag=1 then [br]localstr=getflagstr(stream.position) [br]if ip>= ip2int("255.255.255.0") and ip<=ip2int("255.255.255.255") then [br]localstr=getflagstr(endipoff+21) [br]country=getflagstr(endipoff+12) [br]end if [br]else [br]localstr=getflagstr(endipoff+8) [br]end if [br]else [br]country=getflagstr(endipoff+4) [br]localstr=getflagstr(stream.position) [br]end if [br]country=trim(country) [br]localstr=trim(localstr) [br]if instr(country,"cz88.net") then country = "ialvin.cn" [br]if instr(localstr,"cz88.net") then localstr = "ialvin.cn" [br]end sub [br][br]private function getflagstr(offset) [br]dim flag [br]flag=0 [br]do while (true) [br]stream.position=offset [br]flag=ascb(stream.read(1)) [br]if(flag=1 or flag=2 ) then [br]buf=stream.read(3) [br]if (flag=2 ) then [br]countryflag=2 [br]endipoff=offset-4 [br]end if [br]offset=ascb(midb(buf,1,1))+(ascb(midb(buf,2,1))*256)+(ascb(midb(buf,3,1))*256*256) [br]else [br]exit do [br]end if [br]loop [br][br]if (offset<12 ) then [br]getflagstr="" [br]else [br]stream.position=offset [br]getflagstr=getstr() [br]end if [br]end function [br][br]private function getstr() [br]dim c [br]getstr="" [br]do while (true) [br]c=ascb(stream.read(1)) [br]if (c=0) then exit do [br][br]if c>127 then [br]if stream.eos then exit do [br]getstr=getstr&chr(ascw(chrb(ascb(stream.read(1)))&chrb(c))) [br]else [br]getstr=getstr&chr(c) [br]end if [br]loop [br]end function [br][br]public function qqwry(dotip) [br]dim ip,nret [br]dim rangb,range,recno [br]ip=ip2int(dotip) [br]set stream=createobject("adodb.stream") [br]stream.mode=3 [br]stream.type=1 [br]stream.open [br]stream.loadfromfile qqwryfile [br]stream.position=0 [br]buf=stream.read(8) [br]firststartip=ascb(midb(buf,1,1))+(ascb(midb(buf,2,1))*256)+(ascb(midb(buf,3,1))*256*256)+(ascb(midb(buf,4,1))*256*256*256) [br]laststartip=ascb(midb(buf,5,1))+(ascb(midb(buf,6,1))*256)+(ascb(midb(buf,7,1))*256*256)+(ascb(midb(buf,8,1))*256*256*256) [br]recordcount=int((laststartip-firststartip)/7) [br]if (recordcount<=1) then [br]country="unknow" [br]qqwry=2 [br]exit function [br]end if [br]rangb=0 [br]range=recordcount [br]do while (rangb<(range-1)) [br]recno=int((rangb+range)/2) [br]call getstartip (recno) [br]if (ip=startip) then [br]rangb=recno [br]exit do [br]end if [br]if (ip>startip) then [br]rangb=recno [br]else [br]range=recno [br]end if [br]loop [br]call getstartip(rangb) [br]call getendip() [br]if (startip<=ip) and ( endip>=ip) then [br][br]nret=0 [br]else [br][br]nret=3 [br]end if [br]call getcountry(ip) [br]qqwry=nret [br]end function [br][br]private sub class_terminate() [br]on error resume next [br]stream.close [br]if err then err.clear [br]set stream=nothing [br]end sub [br]end class [br][br][br][br]function look_ip(path,ip) [br]dim wry, iptype, qqwryversion, ipcounter [br]set wry = new tqqwry [br]wry.setpath path [br]on error resume next [br]iptype = wry.qqwry(ip) [br]look_ip = wry.country & " - " & wry.localstr [br]if err then [br]err.clear [br]look_ip = "查詢出錯(cuò)" [br]end if [br]end function [br]%>[br][br]下面是查詢代碼,請(qǐng)保存為search.asp [br] [br]<% [br]dim ip [br]ip=request.querystring("ip") [br][br]if trim(ip)="" then [br]ip=request.servervariables("remote_addr") [br]elseif ubound(split(trim(ip),"."))<>3 then [br]ip=request.servervariables("remote_addr") 'ip地址 [br]end if [br]ipadd=look_ip("cz.dat",ip) '這里注意,數(shù)據(jù)庫(kù)文件名是cz.dat [br]response.write ipadd [br]%>
該文章在 2010/7/13 18:46:00 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |