<?php
/*判断传递的变量是否含有非法字符
如:$_POST/$_GET
功能:SQL防注入系统
*/

//屏蔽错误提示
error_reporting(7);

//需要过滤的字符

$ArrFiltrate = array("'", "or", "and", "union", "where"); //1.如果规则不完全,也会引起安全问题

//出错跳转的URL
$StrGoUrl = ""; //2.如果此处不初始化,且register_globals = On,就会产生变量覆盖漏洞(php4.2.0之前默认register_globals = On)

//防注入函数
function FunStringExist($StrFiltrate, $ArrFiltrate)
{
foreach ($ArrFiltrate as $key => $value)
{
if (stristr($value, $StrFiltrate)) //3.保证or/Or/oR/OR之类的提交不会成功(这是个比较安全的防止SQL注入的函数,现对于strstr()函数而言)
{
return true;
}
}

return false;
}

//合并$_POST && $_GET
if (function_exists(array_merge))
{
$ArrPostAndGet = array_merge ($_POST, $_GET);
}
else
{
//手动合并$_POST
foreach ($_POST as $key => $value)
{
$ArrPostAndGet[] = $value;
}

//手动合并$_GET
foreach ($_GET as $key => $value)
{
$ArrPostAndGet[] = $value;
}
}

//调用防注入函数
foreach ($ArrPostAndGet as $key => $value)
{
if (FunStringExist ($value, $ArrFiltrate))
{
echo "<script>alert('非法字符');</script>";

if (empty($StrGoUrl))
{
echo "<script>history.go(-1);</script>";
}
else
{
echo "<script>window.location = '".$StrGoUrl."'</script>";
}

exit(0);
}
}
?>

posted on 2014-11-10 22:48  milantgh  阅读(889)  评论(0编辑  收藏  举报