5D艺术网首页
商城
|
资讯
|
作品
|
博客
|
教程
|
论坛
登录
注册
加为好友
发短消息
来自:
性别:秘密
最后登录:2008-10-28
http://kafeichong.5d.cn/
首页
|
新闻
|
话题
|
博客
|
相册
|
艺术作品
|
社交关系
|
留言板
|
社交圈
2005/04/14 | 过滤特殊字符和处理数字(转载)
类别(asp)
|
评论
(1)
|
阅读(676)
|
发表于 00:25
来一个能过滤特殊字符和处理数字的增强型字符串过滤函数,可能考虑得不完善,请指教。要防止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)," ")
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") '作为普通字符串过滤
0
评论
Comments
日志分类
首页
[223]
javascript
[29]
asp
[62]
常识
[12]
SQL
[8]
文学
[2]
Dreamweaver
[22]
网页设计
[25]
flash
[20]
片言碎语
[21]
php
[6]
web standard
[16]