简述:/*************************
说明:
判断传递的变量中是否含有非法字符
如$_POST、$_GET
功能:防注入
**************************/
<?php <br><br>//要过滤的非法字符 <BR>$ArrFiltrate=array("'",";","union"); <BR>//出错后要跳转的url,不填则默认前一页 <BR>$StrGoUrl=""; <BR>//是否存在数组中的值 <BR>function FunStringExist($StrFiltrate,$ArrFiltrate){ <BR>foreach ($ArrFiltrate as $key=>$value){ <BR> if (eregi($value,$StrFiltrate)){ <BR> return true; <BR> } <BR>} <BR>return false; <BR>} <br><br>//合并$_POST 和 $_GET <BR>if(function_exists(array_merge)){ <BR> $ArrPostAndGet=array_merge($HTTP_POST_VARS,$HTTP_GET_VARS); <BR>}else{ <BR> foreach($HTTP_POST_VARS as $key=>$value){ <BR> $ArrPostAndGet[]=$value; <BR> } <BR> foreach($HTTP_GET_VARS as $key=>$value){ <BR> $ArrPostAndGet[]=$value; <BR> } <BR>} <br><br>//验证开始 <BR>foreach($ArrPostAndGet as $key=>$value){ <BR> if (FunStringExist($value,$ArrFiltrate)){ <BR> echo "<script language=\"javascript\">alert(\"非法字符\");</script>"; <BR> if (emptyempty($StrGoUrl)){ <BR> echo "<script language=\"javascript\">history.go(-1);</script>"; <BR> }else{ <BR> echo "<script language=\"javascript\">window.location=\"".$StrGoUrl."\";</script>"; <BR> } <BR> exit; <BR> } <BR>} <BR>?> <BR>
保存为checkpostandget.php
然后在每个php文件前加include(“checkpostandget.php“);即可
方法2
/* 过滤所有GET过来变量 */ <BR>foreach ($_GET as $get_key=>$get_var) <BR>{ <BR>if (is_numeric($get_var)) { <BR> $get[strtolower($get_key)] = get_int($get_var); <BR>} else { <BR> $get[strtolower($get_key)] = get_str($get_var); <BR>} <BR>} <br><br>/* 过滤所有POST过来的变量 */ <BR>foreach ($_POST as $post_key=>$post_var) <BR>{ <BR>if (is_numeric($post_var)) { <BR> $post[strtolower($post_key)] = get_int($post_var); <BR>} else { <BR> $post[strtolower($post_key)] = get_str($post_var); <BR>} <i>·本2文来源gaodai$ma#com搞$代*码网2</i><strong>搞gaodaima代码</strong> <BR>} <br><br>/* 过滤函数 */ <BR>//整型过滤函数 <BR>function get_int($number) <BR>{ <BR> return intval($number); <BR>} <BR>//字符串型过滤函数 <BR>function get_str($string) <BR>{ <BR> if (!get_magic_quotes_gpc()) { <BR>return addslashes($string); <BR> } <BR> return $string; <BR>}<BR>