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.writecheckip(ip,allowip1,allowip2)
functioncheckip(ip,allowip1,allowip2)
dimcheck(4)
checkip=false
ipstr=split(ip,".")
allow1=split(allowip1,".")
allow2=split(allowip2,".")
ifcint(allow1(0))>cint(allow2(0))then
'判断IP地址段是否合法
response.write"IP地址段出错!"
exitfunction
endif
fori=0toubound(ipstr)
ifcint(allow1(i))<cint(allow2(i))then
ifcint(allow1(i))=cint(ipstr(i))then
check(i)=true
checkip=true
exitfor
elseifcint(ipstr(i))<cint(allow2(i))then
check(i)=true
checkip=true
exitfor
elseifcint(ipstr(i))>cint(allow2(i))then
check(i)=false
checkip=false
exitfor
else
check(i)=true
checkip=true
endif
endif
endif
elseifcint(allow1(i))>cint(ipstr(i))orcint(allow1(i))<cint(ipstr(i))then
check(i)=false
checkip=false
ifi<>ubound(ipstr)then
exitfor
endif
else
check(i)=true
endif
endif
next
if(check(0)=trueandcheck(1)=trueandcheck(2)=trueandcheck(3)=false)and(cint(allow2(2))>cint(ipstr(2)))then
checkip=true
endif
endfunction
%>
<%
'列举使用HTML表单提交的所有值
ForEachitemInRequest.Form
Response.WriteRequest.Form(item)
Next
%>
列举使用HTML表单提交的所有值
利用ASP得到图片尺寸大小
<%
imgpath="default_22.gif"
setpp=newimgInfo
w=pp.imgW(server.mappath(imgpath))
h=pp.imgH(server.mappath(imgpath))
setpp=nothing
response.write"<imgsrc='"&imgpath&"'border=0><br>宽:"&w&";高:"&h
ClassimgInfo
dimaso
PrivateSubClass_Initialize
setaso=CreateObject("Adodb.Stream")
aso.Mode=3
aso.Type=1
aso.Open
EndSub
PrivateSubClass_Terminate
err.clear
setaso=nothing
EndSub
PrivateFunctionBin2Str(Bin)
DimI,Str
ForI=1toLenB(Bin)
clow=MidB(Bin,I,1)
ifASCB(clow)<128then
Str=Str&Chr(ASCB(clow))
else
I=I+1
ifI<=LenB(Bin)thenStr=Str&Chr(ASCW(MidB(Bin,I,1)&clow))
endif
Next
Bin2Str=Str
EndFunction
PrivateFunctionNum2Str(num,base,lens)
dimret
ret=""
while(num>=base)
ret=(nummodbase)&ret
num=(num-nummodbase)/base
wend
Num2Str=right(string(lens,"0")&num&ret,lens)
EndFunction
PrivateFunctionStr2Num(str,base)
dimret
ret=0
fori=1tolen(str)
ret=ret*base+cint(mid(str,i,1))
next
Str2Num=ret
EndFunction
PrivateFunctionBinVal(bin)
dimret
ret=0
fori=lenb(bin)to1step-1
ret=ret*256+ascb(midb(bin,i,1))
next
BinVal=ret
EndFunction
PrivateFunctionBinVal2(bin)
dimret
ret=0
fori=1tolenb(bin)
ret=ret*256+ascb(midb(bin,i,1))
next
BinVal2=ret
EndFunction
PrivateFunctiongetImageSize(filespec)
dimret(3)
aso.LoadFromFile(filespec)
bFlag=aso.read(3)
selectcasehex(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)):loopwhilep1=255andnotaso.EOS
ifp1>191andp1<196thenexitdoelseaso.read(binval2(aso.Read(2))-2)
do:p1=binVal(aso.Read(1)):loopwhilep1<255andnotaso.EOS
loopwhiletrue
aso.Read(3)
ret(0)="JPG"
ret(2)=binval2(aso.Read(2))
ret(1)=binval2(aso.Read(2))
caseelse:
ifleft(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)=""
endif
endselect
ret(3)="width="""&ret(1)&"""height="""&ret(2)&""""
getimagesize=ret
EndFunction
PublicFunctionimgW(pic_path)
Setfso1=server.CreateObject("Scripting.FileSystemObject")
If(fso1.FileExists(pic_path))Then
Setf1=fso1.GetFile(pic_path)
ext=fso1.GetExtensionName(pic_path)
selectcaseext
case"gif","bmp","jpg","png":
arr=getImageSize(f1.path)
imgW=arr(1)
endselect
Setf1=nothing
else
imgW=0
Endif
Setfso1=nothing
EndFunction
PublicFunctionimgH(pic_path)
Setfso1=server.CreateObject("Scripting.FileSystemObject")
If(fso1.FileExists(pic_path))Then
Setf1=fso1.GetFile(pic_path)
ext=fso1.GetExtensionName(pic_path)
selectcaseext
case"gif","bmp","jpg","png":
arr=getImageSize(f1.path)
imgH=arr(2)
endselect
Setf1=nothing
else
imgH=0
Endif
Setfso1=nothing
EndFunction
EndClass
%>
客户端屏幕分辨率:Request.SERVERVARIABLES("HTTP_UA_PIXELS")
如何判断URL格式是否符合规范?
<%functioncheckisUrl(tmpString)
dimc,icheckisUrl=truetmpString=Lcase(trim(tmpString))ifleft(tmpString,7)<>"http://"thentmpStri...//"&tmpStringfori=8toLen(checkisUrl)c=Lcase(Mid(tmpString,i,1))ifInStr("abcdefghijklmnopqrstuvwxyz_-./",c)<=0andnotIsNumeric(c)thencheckisUrl=falseexitfunctionendifnextifLeft(tmpString,1)="."orRight(tmpString,1)="."thencheckisUrl=falseexitfunctionendififInStr(tmpString,".")<=0thencheckisUrl=falseresponse.Write"f3"exitfunctionendififInStr(checkisUrl,"..")>0thencheckisUrl=falseendif
endfunction%><%
ifcheckisUrl(request("u"))=truethen%>恭喜,你的URL通过!<%else%>对不起,你的URL不合乎规范,请重新检查!<%endif%>
如何利用数据库内容建立一个下拉式列表?
<%myDSN="DSN=xur;uid=xur;pwd=xur"mySQL="select*fromauthorswhereAU_ID<100"setconntemp=server.createobject("adodb.connection")conntemp.openmyDSNsetrstemp=conntemp.execute(mySQL)ifrstemp.eofthenresponse.write"噢,数据库为空!"response.writemySQLconntemp.closesetconntemp=nothingresponse.endendif%><%dountilrstemp.eof%><%rstemp.movenextlooprstemp.closesetrstemp=nothingconntemp.closesetconntemp=nothing'清空对象%>
'获取用户真实IP函数
FunctionGetIP()
GetIP=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
IfGetIP=""ThenGetIP=Request.ServerVariables("REMOTE_ADDR")
EndFunction
'获取完整地址栏地址
FunctionGetUrl()
GetUrl="http://"&Request.ServerVariables("SERVER_NAME")&Request.ServerVariables("URL")
IfRequest.ServerVariables("QUERY_STRING")<>""ThenGetURL=GetUrl&"?"&Request.ServerVariables("QUERY_STRING")
EndFunction
'获取本页文件名
FunctionSelfName()
SelfName=Mid(Request.ServerVariables("URL"),InstrRev(Request.ServerVariables("URL"),"/")+1)
EndFunction
'获取文件后缀名
FunctionGetExt(filename)
GetExt=Mid(filename,InstrRev(filename,".")+1)
EndFunction
'求字符串长度函数
FunctionGetLength(str)
Dimi,length
Fori=1toLen(str)
IfAsc(Mid(str,i,1))<0orAsc(Mid(str,i,1))>256Then
length=length+2
Else
length=length+1
EndIf
Next
GetLength=length
EndFunction
'过滤不良字符
FunctionChkBadWords(fString)
DimBadWords,bwords,i
BadWords="我操|操你|操他|你妈的|他妈的|狗|杂种|屄|屌|王八|强奸|做爱|处女|泽民|法轮|法伦|洪志|法輪"
IfNot(IsNull(BadWords)orIsNull(fString))Then
bwords=Split(BadWords,"|")
Fori=0toUBound(bwords)
fString=Replace(fString,bwords(i),string(Len(bwords(i)),"*"))
Next
ChkBadWords=fString
EndIf
EndFunction
'防止外部提交
FunctionChkPost()
DimURL1,URL2
ChkPost=False
URL1=Cstr(Request.ServerVariables("HTTP_REFERER"))
URL2=Cstr(Request.ServerVariables("SERVER_NAME"))
IfMid(URL1,8,Len(URL2))<>URL2Then
ChkPost=False
Else
ChkPost=True
EndIf
EndFunction
'过滤HTML字符函数
FunctionHTMLEncode(fString)
IfNotIsNull(fString)AndfString<>""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
EndIf
EndFunction
'清除HTML标记
FunctionstripHTML(strHTML)
DimobjRegExp,strOutput
SetobjRegExp=NewRegexp
objRegExp.IgnoreCase=True
objRegExp.Global=True
objRegExp.Pattern="<.+?>"
strOutput=objRegExp.Replace(strHTML,"")
strOutput=Replace(strOutput,"<","<")
strOutput=Replace(strOutput,">",">")
stripHTML=strOutput
SetobjRegExp=Nothing
EndFunction