php過濾特殊危險字符的總結
一般,對於傳進來的字符,php教程可以用addslashes函數處理一遍(要get_magic_quotes_gpc()為假才處理,不然就重復轉義了!),這樣就能達到一定程度的安全要求
比如這樣
代碼如下 復制代碼
if (!get_magic_quotes_gpc()) {
add_slashes($_GET);
add_slashes($_POST);
add_slashes($_COOKIE);
}
function add_slashes($string) {
if (is_array($string)) {
foreach ($string as $key => $value) {
$string[$key] = add_slashes($value);
}
} else {
$string = addslashes($string);
}
return $string;
}
但是還可以更進一步進行重新編碼,解碼,如下:
代碼如下 復制代碼
//編碼
function htmlencode($str) {
if(empty($str)) return;
if($str=="") return $str;
$str=trim($str);
$str=str_replace("&","&",$str);
$str=str_replace(">",">",$str);
$str=str_replace("<","&lt;",$str);
$str=str_replace(chr(32),"&nbsp;",$str);
$str=str_replace(chr(9),"&nbsp;",$str);
$str=str_replace(chr(34),"&",$str);
$str=str_replace(chr(39),"&#39;",$str);
$str=str_replace(chr(13),"<br />",$str);
$str=str_replace("'","''",$str);
$str=str_replace("select","sel&#101;ct",$str);
$str=str_replace("join","jo&#105;n",$str);
$str=str_replace("union","un&#105;on",$str);
$str=str_replace("where","wh&#101;re",$str);
$str=str_replace("insert","ins&#101;rt",$str);
$str=str_replace("delete","del&#101;te",$str);
$str=str_replace("update","up&#100;ate",$str);
$str=str_replace("like","lik&#101;",$str);
$str=str_replace("drop","dro&#112;",$str);
$str=str_replace("create","cr&#101;ate",$str);
$str=str_replace("modify","mod&#105;fy",$str);
$str=str_replace("rename","ren&#097;me",$str);
$str=str_replace("alter","alt&#101;r",$str);
$str=str_replace("cast","ca&#115;",$str);
return $str;
}
這樣就能更放心的對外來數據進行入庫處理了, 但是從數據庫取出來,在前台顯示的時候,必須重新解碼一下:
代碼如下 復制代碼
//解碼
function htmldecode($str) {
if(empty($str)) return;
if($str=="") return $str;
$str=str_replace("sel&#101;ct","select",$str);
$str=str_replace("jo&#105;n","join",$str);
$str=str_replace("un&#105;on","union",$str);
$str=str_replace("wh&#101;re","where",$str);
$str=str_replace("ins&#101;rt","insert",$str);
$str=str_replace("del&#101;te","delete",$str);
$str=str_replace("up&#100;ate","update",$str);
$str=str_replace("lik&#101;","like",$str);
$str=str_replace("dro&#112;","drop",$str);
$str=str_replace("cr&#101;ate","create",$str);
$str=str_replace((www.111cn.net)"mod&#105;fy","modify",$str);
$str=str_replace("ren&#097;me","rename",$str);
$str=str_replace("alt&#101;r","alter",$str);
$str=str_replace("ca&#115;","cast",$str);
$str=str_replace("&amp;","&",$str);
$str=str_replace("&gt;",">",$str);
$str=str_replace("&lt;","<",$str);
$str=str_replace("&nbsp;",chr(32),$str);
$str=str_replace("&nbsp;",chr(9),$str);
$str=str_replace("&",chr(34),$str);
$str=str_replace("&#39;",chr(39),$str);
$str=str_replace("<br />",chr(13),$str);
$str=str_replace("''","'",$str);
return $str;
}
雖然多了一步編碼,解碼的過程,但是安全方面,會更進一步,要如何做,自己取捨吧。
再附一些
代碼如下 復制代碼
function safe_replace($string) {
$string = str_replace(' ','',$string);
$string = str_replace(''','',$string);
$string = str_replace(''','',$string);
$string = str_replace('*','',$string);
$string = str_replace('"','"',$string);
$string = str_replace("'",'',$string);
$string = str_replace('"','',$string);
$string = str_replace(';','',$string);
$string = str_replace('<','<',$string);
$string = str_replace('>','>',$string);
$string = str_replace("{",'',$string);
$string = str_replace('}','',$string);
return $string;
}
更全面的
代碼如下 復制代碼
//處理提交的數據
function htmldecode($str) {
if (empty ( $str ) || "" == $str) {
return "";
}
$str = strip_tags ( $str );
$str = htmlspecialchars ( $str );
$str = nl2br ( $str );
$str = str_replace ( "?", "", $str );
$str = str_replace ( "*", "", $str );
$str = str_replace ( "!", "", $str );
$str = str_replace ( "~", "", $str );
$str = str_replace ( "$", "", $str );
$str = str_replace ( "%", "", $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 ( "cast", "", $str );
$farr = array ("//s+/", //過濾多余的空白
"/<(//?)(img|script|i?frame|style|html|body|title|link|meta|/?|/%)([^>]*?)>/isU", //過濾 <script 防止引入惡意內容或惡意代碼,如果不需要插入flash等,還可以加入