PHP防注入安全代码


Posted in PHP onApril 09, 2008

简述:/*************************   
说明:   
判断传递的变量中是否含有非法字符   
如$_POST、$_GET   
功能:防注入   
**************************/      

<?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 "<script language=\"javascript\">alert(\"非法字符\");</script>";      
    if (emptyempty($StrGoUrl)){      
    echo "<script language=\"javascript\">history.go(-1);</script>";      
    }else{      
    echo "<script language=\"javascript\">window.location=\"".$StrGoUrl."\";</script>";      
    }      
    exit;      
  }      
}      
?> 
    

保存为checkpostandget.php     
然后在每个php文件前加include(“checkpostandget.php“);即可     

方法2     

/* 过滤所有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;      
}
       
PHP 相关文章推荐
从零开始 教你如何搭建Discuz!4.1论坛
Jul 07 PHP
我的论坛源代码(七)
Oct 09 PHP
php中实现简单的ACL 完结篇
Sep 07 PHP
destoon整合UCenter图文教程
Jun 21 PHP
php实现按指定大小等比缩放生成上传图片缩略图的方法
Dec 15 PHP
在Linux系统的服务器上隐藏PHP版本号的方法
Jun 06 PHP
php实现网站留言板功能
Nov 04 PHP
php 三大特点:封装,继承,多态
Feb 19 PHP
PHP实现活动人选抽奖功能
Apr 19 PHP
phpstudy的php版本自由修改的方法
Oct 18 PHP
详细解读php的命名空间(一)
Feb 21 PHP
PHP cURL获取微信公众号access_token的实例
Apr 28 PHP
PHP中用header图片地址 简单隐藏图片源地址
Apr 09 #PHP
PHP中的extract的作用分析
Apr 09 #PHP
如何在PHP程序中防止盗链
Apr 09 #PHP
php的access操作类
Apr 09 #PHP
php时间不正确的解决方法
Apr 09 #PHP
php Ajax乱码
Apr 09 #PHP
PHP提取中文首字母
Apr 09 #PHP
You might like
PHP+XML 制作简单的留言本 图文教程
2009/11/02 PHP
PHP 基于文件头的文件类型验证类函数
2012/05/01 PHP
php中fgetcsv()函数用法实例
2014/11/28 PHP
PHP中文编码小技巧
2014/12/25 PHP
Windows下Apache + PHP SESSION丢失的解决过程全纪录
2015/04/07 PHP
php实现表单多按钮提交action的处理方法
2015/10/24 PHP
PHP创建自己的Composer包方法
2018/04/09 PHP
列表内容的选择
2006/06/30 Javascript
JSF中confirm弹出框的用法示例介绍
2014/01/07 Javascript
jquery实现导航固定顶部的效果仿蘑菇街
2014/10/22 Javascript
flash+jQuery实现可关闭及重复播放的压顶广告
2015/04/15 Javascript
javascript实现根据时间段显示问候语的方法
2015/06/18 Javascript
基于JavaScript实现生成名片、链接等二维码
2015/09/20 Javascript
webpack-mvc 传统多页面组件化开发详解
2019/05/07 Javascript
简单学习5种处理Vue.js异常的方法
2019/06/17 Javascript
Python实现测试磁盘性能的方法
2015/03/12 Python
通过Python来使用七牛云存储的方法详解
2015/08/07 Python
python3批量删除豆瓣分组下的好友的实现代码
2016/06/07 Python
利用Python实现网络测试的脚本分享
2017/05/26 Python
Python实现自定义函数的5种常见形式分析
2018/06/16 Python
python实现n个数中选出m个数的方法
2018/11/13 Python
Django --Xadmin 判断登录者身份实例
2020/07/03 Python
施华洛世奇德国官网:SWAROVSKI德国
2017/02/01 全球购物
英国剑桥包中文官网:The Cambridge Satchel Company中国
2018/11/06 全球购物
Java的接口和C++的虚类的相同和不同处
2014/03/27 面试题
网络工程师面试(三木通信技术有限公司)
2013/06/05 面试题
几个判断型的面试题
2012/07/03 面试题
房地产销售经理岗位职责
2014/01/01 职场文书
幼儿园教师考核制度
2014/02/01 职场文书
产假请假条
2014/04/10 职场文书
大专毕业生求职信
2014/07/05 职场文书
社团招新宣传语
2015/07/13 职场文书
2016教师政治学习心得体会
2016/01/23 职场文书
pytorch 梯度NAN异常值的解决方案
2021/06/05 Python
Android在Sqlite3中的应用及多线程使用数据库的建议
2022/04/24 Java/Android
Spring Cloud OAuth2实现自定义token返回格式
2022/06/25 Java/Android