background image

PHP 防注入函数代码总结

在 php 中防注入一般会写一个全局文件用来过滤特殊的字符串,本文章来总结了各种各
样的 php 防注入函数代码,同时还可防 sql 注入大家可参考。

为了安全,我们常用到下面的函数来过滤一些传递过来的非法字符:

PHP 防注入函数

 

 

 

代码如下 复制代码

<?php
//要过滤的非法字符
$ArrFiltrate=array(“‘”,”;”,”union”,”select”,”delete”,”‘”,”or”,”and”,”=”);
//出错后要跳转的 url,不填则默认前一页
$StrGoUrl=”";
//是否存在数组中的值
function FunStringExist($StrFiltrate,$ArrFiltrate){
foreach ($ArrFiltrate as $key=>$value){
if (eregi($value,$StrFiltrate)){
return true;
}
}
return false;
}
//合并$_POST 

 

和 $_GET

if(function_exists(array_merge)){
$ArrPostAndGet=array_merge($HTTP_POST_VARS,$HTTP_GET_VARS);
}else{
foreach($HTTP_POST_VARS as $key=>$value){
$ArrPostAndGet[]=$value;
}
foreach($HTTP_GET_VARS as $key=>$value){
$ArrPostAndGet[]=$value;
}
}
//验证开始
foreach($ArrPostAndGet as $key=>$value){
if (FunStringExist($value,$ArrFiltrate)){
if (empty($StrGoUrl)){
echo “<script language=”javascript”>history.go(-1);</script>”;
}else{
echo “<script language=”javascript”>window.location=”".$StrGoUrl.””;</script>”;
}
exit;