咖啡虫之家

我的日历
友情链接
访问计数
最新评论
获取RSS
我的 Blog:
kafeichong 最新的 20 条日志
[javascript]
[asp]
[常识]
[SQL]
[文学]
[Dreamweaver]
[网页设计]
[flash]
[片言碎语]
[php]
[web standard]
全站 Blog:
全站最新的 20 条日志

过滤特殊字符和处理数字(转载)

Author:咖啡虫 PublishTime:2005-4-14
来一个能过滤特殊字符和处理数字的增强型字符串过滤函数,可能考虑得不完善,请指教。要防止SQL注入,除了过滤单引号和逗号,还需要过滤其它什么字符吗?过滤分号是否显得多余?
另外,对单引号的处理有几种,一是直接去掉,一是将一个单引号变两个,我这里将英文单引号过滤为中文单引号,不知道有没有问题?

<%
'对输入的数据进行处理,防止异常错误和SQL注入攻击
'strtype的解释:n->数字,t->段落,其它->普通单行字符串
Function safeinput(text,strtype)
dim tempstr
tempstr = text
tempstr = replace(trim(tempstr),";",";") '过滤分号
tempstr = replace(trim(tempstr),"'","’") '过滤单引号
tempstr = replace(trim(tempstr),",",",") '过滤英文逗号
if strtype = "n" then '过滤数字
tempstr = replace(trim(tempstr),",","") '去掉数字当中可能出现的逗号分隔符,这里是中文逗号。英文的前面已经过滤了。
tempstr = replace(trim(tempstr),"’","") '如果数字中有单引号,前面会替换成中文的单引号。这里把单引号去掉
tempstr = replace(tempstr,"。",".") '过滤句号。有的人在中文输入状态下常把小数点打成中文句号,这里替换成英文小数点
tempstr = replace(tempstr,".",".") '全角的英文句号,同上,过滤之。
if IsNumeric(tempstr) then '判断过滤后的字符串是否是数字,如果是则进行数据类型转换,如果不是,说明还有其它非数字字符,这里统一转变为0
tempstr = Csng(tempstr)
else
tempstr = 0
end if
elseif strtype = "t" then '过滤一段文字。一段文字存入数据库需要转换空格和换行,也一并放在这里处理。
tempstr=replace(tempstr,chr(13),"<br>")
tempstr=replace(tempstr,chr(32),"&nbsp;")
end if
safeinput = tempstr
End Function
%>

调用方法:
dim newstr,newtext,newnum
newnum = safeinput(request.Form("oldnum"),"n") '作为数字过滤
newtext = safeinput(request.Form("oldtext"),"t") '作为段落文字过滤
newstr = safeinput(request.Form("oldstr"),"s") '作为普通字符串过滤
分类于:asp

 

Powered by 5DBlog.com