当前位置:Gxlcms > PHP教程 > 实用的php防注入类代码实例详解

实用的php防注入类代码实例详解

时间:2021-07-01 10:21:17 帮助过:25人阅读

这篇文章主要介绍了简单实用的PHP防注入类实例,以两个简单的防注入类为例介绍了PHP防注入的原理与技巧,对网站安全建设来说非常具有实用价值,需要的朋友可以参考下

本文实例讲述了简单实用的PHP防注入类。分享给大家供大家参考。具体如下:

PHP防注入注意要过滤的信息基本是get,post,然后对于sql就是我们常用的查询,插入等等sql命令了,下面我给各位整理两个简单的例子,希望这些例子能给你网站带来安全.

PHP防注入类代码如下:

  1. <?php
  2. /**
  3. * 参数处理类
  4. */
  5. class Params
  6. {
  7. public $get = array();
  8. public $post = array();
  9. function construct()
  10. {
  11. if (!emptyempty($_GET)) {
  12. foreach
  13. ($_GET as $key => $val) {
  14. if (is_numeric($val)) {
  15. $this->get[$key] = $this->getInt($val);
  16. } else {
  17. $this->get[$key] = $this->getStr($val);
  18. }
  19. }
  20. }
  21. if (!emptyempty($_POST)) {
  22. foreach ($_POST as $key => $val) {
  23. if (is_numeric($val)) {
  24. $this->post[$key] = $this->getInt($val);
  25. } else {
  26. $this->post[$key] = $this->getStr($val);
  27. }
  28. }
  29. }
  30. }
  31. public function getInt($number)
  32. {
  33. return intval($number);
  34. }
  35. public function getStr($string)
  36. {
  37. if (!
  38. get_magic_quotes_gpc
  39. ()) {
  40. $string =
  41. addslashes
  42. ($string);
  43. }
  44. return $string;
  45. }
  46. public function checkInject($string)
  47. {
  48. return eregi('select|insert|update|delete|/*|*|../|./|union|into|load_file|outfile', $string);
  49. }
  50. public function verifyId($id = null)
  51. {
  52. if (!$id || $this->checkInject($id) || !is_numeric($id)) {
  53. $id = false;
  54. } else {
  55. $id = intval($id);
  56. }
  57. return $id;
  58. }
  59. }
  60. ?>


例子二,代码如下:

  1. <?php
  2. /*************************
  3. 说明:
  4. 判断传递的
  5. 变量
  6. 中是否含有非法字符
  7. 如$_POST、$_GET
  8. 功能:
  9. 防注入
  10. *************************/
  11. //要过滤的非法字符
  12. $ArrFiltrate=array("'","or","and","union","where");
  13. //出错后要跳转的url,不填则默认前一页
  14. $StrGoUrl="";
  15. //是否存在数组中的值
  16. function FunStringExist($StrFiltrate,$ArrFiltrate){
  17. foreach ($ArrFiltrate as $key=>$value){
  18. if (eregi($value,$StrFiltrate)){
  19. return true;
  20. }
  21. }
  22. return false;
  23. }
  24. //合并$_POST 和 $_GET
  25. if(function_exists(
  26. array_merge
  27. )){
  28. $ArrPostAndGet=array_merge($HTTP_POST_VARS,$HTTP_GET_VARS);
  29. }else{
  30. foreach($HTTP_POST_VARS as $key=>$value){
  31. $ArrPostAndGet[]=$value;
  32. }
  33. foreach($HTTP_GET_VARS as $key=>$value){
  34. $ArrPostAndGet[]=$value;
  35. }
  36. }
  37. //验证开始
  38. foreach($ArrPostAndGet as $key=>$value){
  39. if (FunStringExist($value,$ArrFiltrate)){
  40. echo "<script language='
  41. javascript
  42. '>alert('传递的信息中不得包含{',or,and,union}等非法字符请您把他们换成{‘,OR,AND,UNION}');</script>";
  43. if (emptyempty($StrGoUrl)){
  44. echo "<scriptlanguage='javascript'>history.go(-1);</script>";
  45. }else{
  46. echo "<scriptlanguage='javascript'>window.location='".$StrGoUrl."';</script>";
  47. }
  48. exit;
  49. }
  50. }
  51. /***************结束防止PHP注入*****************/
  52. ?>

以上就是实用的php防注入类代码实例详解的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行