作者:loster(oicq:181306) [如果转载,请勿删除此信息,谢谢]
函数名:s_request()
辅助函数:r_reader()
作用:过滤非法字符,防止sql注入。
参数:s_str:被传入的变量的名,类型:字符串
返回值:过滤后的值。
***************************************************************************
const c_sqlstr=",count,user,user,count,1=1,and,2=2" 需要过滤的字符串序列,每个字符串之间用“,”分隔
dim reader
function r_reader(r_str,f_str)
dim i
if r_str="" or f_str="" then
exit function
end if
reader=split(r_str,f_str)
for i=0 to ubound(reader,1)
reader(i)=cstr(trim(reader(i)))
next
r_reader=ubound(reader,1)
end function
function s_request(s_str)
dim temp,i
if s_str="" then
exit function
end if
temp=request(s_str)
for i=0 to r_reader(c_sqlstr,",")
temp=replace(temp,cstr(reader(i)),"")
next
temp=replace(temp,chr(34),"")
s_request=cstr(trim(temp))
erase reader
end function
用法:
原来的例如这样的语句:
a=request("a")
现在写成:
a=s_request("a")
即可实现非法字符串过滤。
当然,你也可以写成这样:
function s_request(s_str)
dim temp,i,temp1
if s_str="" then
exit function
end if
temp=cstr(request(s_str))
temp1=temp
for i=0 to r_reader(c_sqlstr,",")
temp=replace(temp,cstr(reader(i)),"")
if temp1<>temp then
response.write ("请不要输入非法字符!")
response.end
end if
next
temp=replace(temp,chr(34),"")
s_request=cstr(trim(temp))
erase reader
end function
这样,可以返回一个错误报告。
