欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一些值得一看的代码asp

程序员文章站 2023-11-13 15:07:40
asp中对ip进行过滤限制函数 <% '获取访问者的地址 ip=request.servervariables("remote_addr") '允许的ip地址段为10...
asp中对ip进行过滤限制函数
<%
'获取访问者的地址
ip=request.servervariables("remote_addr")
'允许的ip地址段为10.0.0.0~10.68.63.255
allowip1="10.0.0.0"
allowip2="10.68.10.71"
response.write checkip(ip,allowip1,allowip2)
function checkip(ip,allowip1,allowip2)
dim check(4)
checkip=false
ipstr=split(ip,".")
allow1=split(allowip1,".")
allow2=split(allowip2,".")
if cint(allow1(0))>cint(allow2(0)) then
'判断ip地址段是否合法
response.write "ip地址段出错!"
exit function
end if
for i=0 to ubound(ipstr)
if cint(allow1(i))<cint(allow2(i)) then
if cint(allow1(i))=cint(ipstr(i)) then
check(i)=true
checkip=true
exit for
elseif cint(ipstr(i))<cint(allow2(i)) then
check(i)=true
checkip=true
exit for
elseif cint(ipstr(i))>cint(allow2(i)) then
check(i)=false
checkip=false
exit for
else
check(i)=true
checkip=true
end if
end if
end if
elseif cint(allow1(i))>cint(ipstr(i)) or cint(allow1(i))<cint(ipstr(i)) then
check(i)=false
checkip=false
if i<>ubound(ipstr) then
exit for
end if
else
check(i)=true
end if
end if
next
if (check(0)=true and check(1)=true and check(2)=true and check(3)=false) and (cint(allow2(2))>cint(ipstr(2))) then
checkip=true
end if
end function
%>


<%
'列举使用html表单提交的所有值
for each item in request.form
     response.write request.form(item)
next
%>
列举使用html表单提交的所有值 

利用asp得到图片尺寸大小 
<%  
imgpath="default_22.gif"

set  pp=new  imginfo  
w = pp.imgw(server.mappath(imgpath))  
h = pp.imgh(server.mappath(imgpath)) 
set pp=nothing 

response.write "<img src='"&imgpath&"' border=0><br>宽:"&w&";高:"&h


class  imginfo  
dim  aso  
private  sub  class_initialize  
   set  aso=createobject("adodb.stream")  
   aso.mode=3    
   aso.type=1    
   aso.open    
end  sub  
private  sub  class_terminate
   err.clear
   set  aso=nothing  
end  sub  

private  function  bin2str(bin)  
   dim  i,  str  
   for  i=1  to  lenb(bin)  
     clow=midb(bin,i,1)  
     if  ascb(clow)<128  then  
       str  =  str  &  chr(ascb(clow))  
     else  
       i=i+1  
       if  i  <=  lenb(bin)  then  str  =  str  &  chr(ascw(midb(bin,i,1)&clow))  
     end  if  
   next    
   bin2str  =  str  
end  function  

private  function  num2str(num,base,lens)  
   dim  ret  
   ret  =  ""  
   while(num>=base)  
     ret  =  (num  mod  base)  &  ret  
     num  =  (num  -  num  mod  base)/base  
   wend  
   num2str  =  right(string(lens,"0")  &  num  &  ret,lens)  
end  function  

private  function  str2num(str,base)  
   dim  ret  
   ret  =  0  
   for  i=1  to  len(str)  
     ret  =  ret  *base  +  cint(mid(str,i,1))  
   next  
   str2num=ret  
end  function  

private  function  binval(bin)  
   dim  ret  
   ret  =  0  
   for  i  =  lenb(bin)  to  1  step  -1  
     ret  =  ret  *256  +  ascb(midb(bin,i,1))  
   next  
   binval=ret  
end  function  

private  function  binval2(bin)  
   dim  ret  
   ret  =  0  
   for  i  =  1  to  lenb(bin)  
     ret  =  ret  *256  +  ascb(midb(bin,i,1))  
   next  
   binval2=ret  
end  function  

private  function  getimagesize(filespec)    
   dim  ret(3)  
   aso.loadfromfile(filespec)  
   bflag=aso.read(3)  
   select  case  hex(binval(bflag))  
   case  "4e5089":  
     aso.read(15)  
     ret(0)="png"  
     ret(1)=binval2(aso.read(2))  
     aso.read(2)  
     ret(2)=binval2(aso.read(2))  
   case  "464947":  
     aso.read(3)  
     ret(0)="gif"  
     ret(1)=binval(aso.read(2))  
     ret(2)=binval(aso.read(2))  
   case  "535746":  
     aso.read(5)  
     bindata=aso.read(1)  
     sconv=num2str(ascb(bindata),2  ,8)  
     nbits=str2num(left(sconv,5),2)  
     sconv=mid(sconv,6)  
     while(len(sconv)<nbits*4)  
       bindata=aso.read(1)  
       sconv=sconv&num2str(ascb(bindata),2  ,8)  
     wend  
     ret(0)="swf"  
     ret(1)=int(abs(str2num(mid(sconv,1*nbits+1,nbits),2)-str2num(mid(sconv,0*nbits+1,nbits),2))/20)  
     ret(2)=int(abs(str2num(mid(sconv,3*nbits+1,nbits),2)-str2num(mid(sconv,2*nbits+1,nbits),2))/20)  
   case  "ffd8ff":  
     do    
       do:  p1=binval(aso.read(1)):  loop  while  p1=255  and  not  aso.eos  
       if  p1>191  and  p1<196  then  exit  do  else  aso.read(binval2(aso.read(2))-2)  
       do:p1=binval(aso.read(1)):loop  while  p1<255  and  not  aso.eos  
     loop  while  true  
     aso.read(3)  
     ret(0)="jpg"  
     ret(2)=binval2(aso.read(2))  
     ret(1)=binval2(aso.read(2))  
   case  else:  
     if  left(bin2str(bflag),2)="bm"  then  
       aso.read(15)  
       ret(0)="bmp"  
       ret(1)=binval(aso.read(4))  
       ret(2)=binval(aso.read(4))  
     else  
       ret(0)=""  
     end  if  
   end  select  
   ret(3)="width="""  &  ret(1)  &"""  height="""  &  ret(2)  &""""  
   getimagesize=ret  
end  function  

public function  imgw(pic_path)  
     set  fso1  =  server.createobject("scripting.filesystemobject")  
     if (fso1.fileexists(pic_path)) then 
   set  f1  =  fso1.getfile(pic_path)  
   ext=fso1.getextensionname(pic_path)  
   select  case  ext  
    case  "gif","bmp","jpg","png":  
     arr=getimagesize(f1.path)  
     imgw = arr(1)  
   end  select  
   set  f1=nothing 
  else
      imgw = 0
  end if   
     set  fso1=nothing  
end  function  

public function  imgh(pic_path)  
     set  fso1  =  server.createobject("scripting.filesystemobject") 
  if (fso1.fileexists(pic_path)) then 
   set  f1  =  fso1.getfile(pic_path)  
   ext=fso1.getextensionname(pic_path)  
   select  case  ext  
    case  "gif","bmp","jpg","png":  
     arr=getimagesize(f1.path)  
     imgh = arr(2)  
   end  select  
   set  f1=nothing  
  else
   imgh = 0 
  end if  
     set  fso1=nothing  
end  function  
end  class
%>
客户端屏幕分辨率:request.servervariables("http_ua_pixels") 

如何判断url格式是否符合规范?
<% function checkisurl(tmpstring)
      dim c,i      checkisurl = true      tmpstring=lcase(trim(tmpstring))      if left(tmpstring,7)<>"http://" then tmpstri ... //"&tmpstring      for i = 8 to len(checkisurl)            c = lcase(mid(tmpstring, i, 1))            if instr("abcdefghijklmnopqrstuvwxyz_-./\", c) <= 0 and not isnumeric(c) then                  checkisurl = false                  exit function            end if      next      if left(tmpstring, 1) = "." or right(tmpstring, 1) = "." then            checkisurl = false            exit function      end if      if instr(tmpstring, ".") <= 0 then            checkisurl = false            response.write "f3"            exit function      end if      if instr(checkisurl, "..") > 0 then            checkisurl = false      end if
end function%><%
if checkisurl(request("u"))=true then      %>恭喜,你的url通过!<%else      %>对不起,你的url不合乎规范,请重新检查!<%end if%>


如何利用数据库内容建立一个下拉式列表? 

<% mydsn="dsn=xur;uid=xur;pwd=xur"mysql="select * from authors where au_id<100"set conntemp=server.createobject("adodb.connection")conntemp.open mydsnset rstemp=conntemp.execute(mysql)if rstemp.eof thenresponse.write "噢,数据库为空!"response.write mysqlconntemp.closeset conntemp=nothingresponse.end  end if%><%do until rstemp.eof %><%rstemp.movenextlooprstemp.closeset rstemp=nothingconntemp.closeset conntemp=nothing' 清空对象%>
'获取用户真实ip函数
function getip()
getip = request.servervariables("http_x_forwarded_for")
if getip = "" then getip = request.servervariables("remote_addr")
end function

'获取完整地址栏地址
function geturl()
geturl="http://"&request.servervariables("server_name")&request.servervariables("url")
if request.servervariables("query_string")<>"" then geturl=geturl&"?"& request.servervariables("query_string")
end function

'获取本页文件名
function selfname()
selfname = mid(request.servervariables("url"),instrrev(request.servervariables("url"),"/")+1)
end function

'获取文件后缀名
function getext(filename)
getext = mid(filename,instrrev(filename,".")+1)
end function

'求字符串长度函数
function getlength(str)
dim i,length
for i = 1 to len(str)
if asc(mid(str,i,1))<0 or asc(mid(str,i,1))>256 then
length = length+2
else
length = length+1
end if
next
getlength = length
end function

'过滤不良字符
function chkbadwords(fstring)
dim badwords,bwords,i
badwords = "我操|操你|操他|你妈的|他妈的|狗|杂种|屄|屌|王八|强奸|做爱|处女|泽民|*|法伦|*|*"
if not(isnull(badwords) or isnull(fstring)) then
bwords = split(badwords, "|")
for i = 0 to ubound(bwords)
fstring = replace(fstring, bwords(i), string(len(bwords(i)),"*"))
next
chkbadwords = fstring
end if
end function

'防止外部提交
function chkpost()
dim url1,url2
chkpost = false
url1 = cstr(request.servervariables("http_referer"))
url2 = cstr(request.servervariables("server_name"))
if mid(url1,8,len(url2))<>url2 then
chkpost = false
else
chkpost = true
end if
end function

'过滤html字符函数
function htmlencode(fstring)
if not isnull(fstring) and fstring <> "" then
fstring = replace(fstring, "&", "&")
fstring = replace(fstring, ">", ">")
fstring = replace(fstring, "<", "<")
fstring = replace(fstring, chr(32), " ")
fstring = replace(fstring, chr(9), "  ")
fstring = replace(fstring, chr(34), """)
fstring = replace(fstring, chr(39), "'")
fstring = replace(fstring, chr(13), "")
fstring = replace(fstring, chr(10) & chr(10), "</p><p>")
fstring = replace(fstring, chr(10), "<br>")
fstring = replace(fstring, chr(255), " ")
htmlencode = fstring
end if
end function

'清除html标记
function striphtml(strhtml)
dim objregexp,stroutput
set objregexp = new regexp
objregexp.ignorecase = true
objregexp.global = true
objregexp.pattern = "<.+?>"
stroutput = objregexp.replace(strhtml,"")
stroutput = replace(stroutput, "<","<")
stroutput = replace(stroutput, ">",">")
striphtml = stroutput
set objregexp = nothing
end function