加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP防注入安全代码

发布时间:2020-12-13 05:23:51 所属栏目:PHP教程 来源:网络整理
导读:简述:/* **** 说明: 判断传递的变量中是否含有非法字符 如$_POST、$_GET 功能:防注入 ** /div class="codetitle" a style="CURSOR: pointer" data="58897" class="copybut" id="copybut58897" onclick="doCopy('code58897')" 代码如下: div class="codebo

简述:/*****
说明:
判断传递的变量中是否含有非法字符
如$_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;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读