A-A+

PHP过滤特殊字符串类可用作前台读取使用。

2012年01月16日 PHP技术文章 暂无评论 阅读 133 views 次

自定义PHP过滤特殊字符类。
class class_replace{
function htmldecode($str){
if(empty($str)) return;
if($str=="") return $str;
//以下是过滤特殊字符串
$str=str_replace("&"," ",$str);
$str=str_replace(">"," ",$str);
$str=str_replace("<"," ",$str);
$str=str_replace("chr(32)"," ",$str);
$str=str_replace("chr(9)"," ",$str);
$str=str_replace("chr(34)"," ",$str);
$str=str_replace("\""," ",$str);
$str=str_replace("chr(39)"," ",$str);
$str=str_replace(""," ",$str);
$str=str_replace("'"," ",$str);
$str=str_replace("select"," ",$str);
$str=str_replace("join"," ",$str);
$str=str_replace("union"," ",$str);
$str=str_replace("where"," ",$str);
$str=str_replace("insert"," ",$str);
$str=str_replace("delete"," ",$str);
$str=str_replace("update"," ",$str);
$str=str_replace("like"," ",$str);
$str=str_replace("drop"," ",$str);
$str=str_replace("create"," ",$str);
$str=str_replace("modify"," ",$str);
$str=str_replace("rename"," ",$str);
$str=str_replace("alter"," ",$str);
$str=str_replace("cas"," ",$str);
$str=str_replace("replace"," ",$str);
$str=str_replace("%"," ",$str);
$str=str_replace("or"," ",$str);
$str=str_replace("and"," ",$str);
$str=str_replace("!"," ",$str);
$str=str_replace("xor"," ",$str);
$str=str_replace("not"," ",$str);
$str=str_replace("user"," ",$str);
$str=str_replace("||"," ",$str);
$str=str_replace("<"," ",$str);
$str=str_replace(">"," ",$str);
return $str;
}
}

评注(使用方法):
$guolv=new class_replace(); 实例化
$username=$guolv->htmldecode($_POST["username"]); 使用例子
这个类来自于互联网,仅供交流使用!
if(empty($str)) return;
if($str=="") return $str;
这两句代码有什么区别,有点不解?
strip_tags — 过滤全部的HTML标记(包括链接)和PHP标记、JS代码等
注册清除:$name = strip_tags($_POST['name']);
同函数:htmlspecialchars:该函数执行后会把提交数据全部按照文本来展示

<script language=javascript>alert('非法字符!');window.location='网址" :window.history.go(-1)
评注(使用方法):可以整理成为一个类,直接引用,建议使用的方法是用转义,将需要的特殊字符进行全部转义读取,不需要的字符进行过滤。

整理常用SQL注入语句插入读取。

1,接收ID过滤:需要用PHP函数 intval($id); 是否数字函数:is_numeric();
2,前台读取数据:strip_tags($text, '<p><a>') $str=str_replace("\n","<br>",str_replace("\r","&nbsp;",strip_tags($text,'<p><a>')));
3,插入数据库安全检测:addslashes 加转义 读取数据,过滤格式:stripslashes 去转义
$pattern = $_POST['name'];
$pattern = addslashes($pattern); 内容。
读取:stripslashes[$pattern];
5,登陆查询:使用 mysql_real_escape_string() 作为用户输入的包装器,就可以避免用户输入中的任何恶意 SQL语句。
6,$homepage = preg_replace("#http:\/\/#", '', $homepage);过滤特列物。

评注(使用方法):比较欣赏的是 intval 这个函数。其它的可以应用在搜索或后台登陆模块上。

标签:

给我留言