加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

通俗易懂的php防注入代码

发布时间:2020-05-24 22:00:43 所属栏目:PHP 来源:互联网
导读:一直搞.net ,最近研究PHP,也涉及到防注入的安全措施,和.net的大同小异,从网上摘回一篇,作为备注,以供自己需要时查阅。

介绍两种方法吧,首先请把以下代码保存为safe.php放在网站根目录下,然后在每个php文件前加include(“/safe.php“);即可 :

php防注入代码方法一:

<div class="codetitle"><a style="CURSOR: pointer" data="98264" class="copybut" id="copybut98264" onclick="doCopy('code98264')"> 代码如下:<div class="codebody" id="code98264">
<?php
//要过滤的非法字符
$ArrFiltrate=array(”‘”,”;”,”union”);
//出错后要跳转的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)){
echo “”;
if (emptyempty($StrGoUrl)){
echo “”;
}else{
echo “”;
}
exit;
}
}
?>

php防注入代码方法二:


<div class="codetitle"><a style="CURSOR: pointer" data="89978" class="copybut" id="copybut89978" onclick="doCopy('code89978')"> 代码如下:<div class="codebody" id="code89978">
/ 过滤所有GET过来变量 /
foreach ($_GET as $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 ($_POST as $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);
}
}
/ 过滤函数 /
//整型过滤函数
function get_int($number)
{
return intval($number);
}
//字符串型过滤函数
function get_str($string)
{
if (!get_magic_quotes_gpc()) {
return addslashes($string);
}
return $string;
}

(编辑:安卓应用网)

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

    推荐文章
      热点阅读