在数据库入库操作前,要对用户提交的数据做检测,如果提交数据项较多,那么检测代码就可能很长,因此,检测代码函数化是比较好的选择,这样提高了代码的重用率,减少了代码的冗余。
本站制作了一个超强入库数据检测过滤函数,代码如下——
'==================================
'=函 数 名:checksql(name,str,attrib,num)
'=功 能:飞腾工作室超强数据检测过滤函数,本站原创
'=使用说明:name-参数名(用于错误显示),str-参数值(用于判断),attrib-参数类型(1为字符型,0为数字型),num-参数值字符数量(字符型中0表示不限制,数字型任何情况下都有限制)
'==================================
Function checksql(name,str,attrib,num)
dim founderr,errmsg
founderr=false
errmsg=""
dim sqlname
sqlname=name
if attrib=1 then
dim Bword(18)
Bword(0)="?"
Bword(1)=";"
Bword(2)=">"
Bword(3)="<"
Bword(4)="-"
Bword(5)="’"
Bword(6)="””"
Bword(7)="&"
Bword(8)="%"
Bword(9)="$"
Bword(10)="'"
Bword(11)=":"
Bword(12)="|"
Bword(13)="("
Bword(14)=")"
Bword(15)="--"
Bword(16)=" chr(9)"
Bword(17)=" chr(34)"
Bword(18)=" chr(32)"
for i= 0 to ubound(Bword)
if instr(str,Bword(i))<>0 then
founderr=true
errmsg=errmsg+"<br><li>"&sqlname&"中含有非法字符(各种符号)!</li>"
end if
next
if str="" then
founderr=true
errmsg=errmsg+"<br><li>"&sqlname&"不能为空!</li>"
elseif num<>0 then
if len(str)>num then
founderr=true
errmsg=errmsg+"<br><li>"&sqlname&"不能超过"&num&"字符!</li>"
end if
end if
elseif attrib=0 then
if str="" then
founderr=true
errmsg=errmsg+"<br><li>"&sqlname&"不能为空!</li>"
elseif not isnumeric(str) then
founderr=true
errmsg=errmsg+"<br><li>"&sqlname&"不是数字型!</li>"
elseif len(str)>num then
founderr=true
errmsg=errmsg+"<br><li>"&sqlname&"数字型参数位数不能超过"&num&"!</li>"
end if
else
founderr=true
errmsg=errmsg+"<br><li>过滤函数调用错误!</li>"
end if
if founderr then
response.write errmsg
response.end
end if
checksql=str
End Function
注意:
本代码中对于没有限制字符数量的内容也作了非法字符检测。
一般来说,不限制字符数量的字段一般都是备注型,如果你需要在这个字段内容中显示那些字符,那么可以把非法字符检测那段代码(即bword段)放于if num<>0 then里面即可,而备注字段的安全检测单独设置。
如果备注字段采用了各种编辑器,那么其安全性由编辑器提供;如果仅使用UBB的话,那么可以加入以下一段:
if num<>0 then
……
else
str = replace(str, ">", ">")
str = replace(str, "<", "<")
str = replace(str, "", "&")
str = Replace(str, CHR(32), " ")
str = Replace(str, CHR(9), " ")
str = Replace(str, CHR(34), """)
str = Replace(str, CHR(39), "'")
str = Replace(str, CHR(13), "")
str = Replace(str, CHR(10) & CHR(10), "</P><P> ")
str = Replace(str, CHR(10), "<BR> ")
end if
其实,代码是次要的,掌握思想和方法才是最重要的
哈哈,忘了举个实例了:
poster=checksql("作者",request.form("poster"),1,10)