ylsunyuan技术论坛中心

 找回密码
 注册(请使用中文注册)
搜索
热搜: 活动 交友 discuz
查看: 751|回复: 0
打印 上一主题 下一主题

简单实用的PHP防注入类实例

[复制链接]

124

主题

127

帖子

619

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
619
跳转到指定楼层
楼主
发表于 2018-6-7 22:32:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

本文实例讲述了简单实用的PHP防注入类。分享给大家供大家参考。具体如下:
PHP防注入注意要过滤的信息基本是get,post,然后对于sql就是我们常用的查询,插入等等sql命令了,下面我给各位整理两个简单的例子,希望这些例子能给你网站带来安全.
PHP防注入类代码如下:
复制代码 代码如下:


  1. <?php
  2. /**
  3. * 参数处理类
  4. * @author JasonWei
  5. */
  6. class Params
  7. {
  8. public $get = array();

  9. public $post = array();

  10. function __construct()
  11. {
  12. if (!emptyempty($_GET)) {
  13. foreach ($_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 (!get_magic_quotes_gpc()) {
  38. $string = addslashes($string);
  39. }
  40. return $string;
  41. }

  42. public function checkInject($string)
  43. {
  44. return eregi('select|insert|update|delete|/*|*|../|./|union|into|load_file|outfile', $string);
  45. }

  46. public function verifyId($id = null)
  47. {
  48. if (!$id || $this->checkInject($id) || !is_numeric($id)) {
  49. $id = false;
  50. } else {
  51. $id = intval($id);
  52. }
  53. return $id;
  54. }
  55. }
  56. ?>

  57. 例子二,代码如下:
  58. 复制代码 代码如下:
  59. <?php
  60. /*************************
  61. 说明:
  62. 判断传递的变量中是否含有非法字符

  63. 如$_POST、$_GET
  64. 功能:
  65. 防注入
  66. *************************/
  67. //要过滤的非法字符
  68. $ArrFiltrate=array("'","or","and","union","where");
  69. //出错后要跳转的url,不填则默认前一页
  70. $StrGoUrl="";
  71. //是否存在数组中的值
  72. function FunStringExist($StrFiltrate,$ArrFiltrate){
  73. foreach ($ArrFiltrate as $key=>$value){
  74. if (eregi($value,$StrFiltrate)){
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. //合并$_POST 和 $_GET
  81. if(function_exists(array_merge)){
  82. $ArrPostAndGet=array_merge($HTTP_POST_VARS,$HTTP_GET_VARS);
  83. }else{
  84. foreach($HTTP_POST_VARS as $key=>$value){
  85. $ArrPostAndGet[]=$value;
  86. }
  87. foreach($HTTP_GET_VARS as $key=>$value){
  88. $ArrPostAndGet[]=$value;
  89. }
  90. }
  91. //验证开始
  92. foreach($ArrPostAndGet as $key=>$value){
  93. if (FunStringExist($value,$ArrFiltrate)){
  94. echo "<script language='javascript'>alert('传递的信息中不得包含{',or,and,union}等非法字符请您把他们换成{‘,OR,AND,UNION}');</script>";
  95. if (emptyempty($StrGoUrl)){
  96. echo "<scriptlanguage='javascript'>history.go(-1);</script>";
  97. }else{
  98. echo "<scriptlanguage='javascript'>window.location='".$StrGoUrl."';</script>";
  99. }
  100. exit;
  101. }
  102. }
  103. /***************结束防止PHP注入*****************/
  104. ?>
复制代码

希望本文所述对大家的PHP程序设计有所帮助。

回复

使用道具 举报

本版积分规则

QQ|Archiver|手机版|小黑屋|ylsunyuan技术论坛 ( 桂ICP备14005218号-1

GMT+8, 2024-4-26 06:32 , Processed in 0.059514 second(s), 31 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表