PHP防注入安全代码

简述:/*****
说明:
判断传递的变量中是否含有非法字符
如$_POST、$_GET
功能:防注入
**/<div class="codetitle"><a style="CURSOR: pointer" data="58897" class="copybut" id="copybut58897" onclick="doCopy('code58897')"> 代码如下:

<div class="codebody" id="code58897">
<?PHP //要过滤的非法字符
$ArrFiltrate=array("'",";","union");
//出错后要跳转的url,不填则认前一页
$StrGoUrl="";
//是否存在数组中的值
functionFunStringExist($StrFiltrate,$ArrFiltrate){
foreach($ArrFiltrateas$key=>$value){
if(eregi($value,$StrFiltrate)){
returntrue;
}
}
returnfalse;
} //合并$_POST和$_GET
if(function_exists(array_merge)){
$ArrPostAndGet=array_merge($HTTP_POST_VARS,$HTTP_GET_VARS);
}else{
foreach($HTTP_POST_VARSas$key=>$value){
$ArrPostAndGet[]=$value;
}
foreach($HTTP_GET_VARSas$key=>$value){
$ArrPostAndGet[]=$value;
}
} //验证开始
foreach($ArrPostAndGetas$key=>$value){
if(FunStringExist($value,$ArrFiltrate)){
echo"<scriptlanguage=\"javascript\">alert(\"非法字符\");";
if(emptyempty($StrGoUrl)){
echo"<scriptlanguage=\"javascript\">history.go(-1);";
}else{
echo"<scriptlanguage=\"javascript\">window.location=\"".$StrGoUrl."\";";
}
exit;
}
}
?>
保存为checkpostandget.PHP
然后在每个PHP文件前加include(“checkpostandget.PHP“);即可

方法2

<div class="codetitle"><a style="CURSOR: pointer" data="50611" class="copybut" id="copybut50611" onclick="doCopy('code50611')"> 代码如下:
<div class="codebody" id="code50611">
/过滤所有GET过来变量/
foreach($_GETas$get_key=>$get_var)
{
if(is_numeric($get_var)){
$get[strtolower($get_key)]=get_int($get_var);
}else{
$get[strtolower($get_key)]=get_str($get_var);
}
} /过滤所有POST过来的变量/
foreach($_POSTas$post_key=>$post_var)
{
if(is_numeric($post_var)){
$post[strtolower($post_key)]=get_int($post_var);
}else{
$post[strtolower($post_key)]=get_str($post_var);
}
} /过滤函数/
//整型过滤函数
functionget_int($number)
{
returnintval($number);
}
//字符串型过滤函数
functionget_str($string)
{
if(!get_magic_quotes_gpc()){
returnaddslashes($string);
}
return$string;
}

PHP防注入

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...