VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 一个简单的PHP防注入类

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

PHP防注入类代码如下:

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

例子二,代码如下:

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

出处:http://www.phpfensi.com/php/20140821/4585.html


相关教程