国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

一個簡單的PHP防注入類

2024-09-04 11:44:06
字體:
來源:轉載
供稿:網友

PHP防注入注意要過濾的信息基本是get,post,然后對于sql就是我們常用的查詢,插入等等sql命令了,下面我給各位整理兩個簡單的例子,希望這些例子能給你網站帶來安全.

PHP防注入類代碼如下:

  1. <?php 
  2. /** 
  3.  * 參數處理類 
  4.  * @author JasonWei 
  5.  */ 
  6. class Params 
  7.     public $get = array(); 
  8.  
  9.     public $post = array(); 
  10.  
  11.     function __construct() 
  12.     { 
  13. if (!emptyempty($_GET)) { 
  14.     foreach ($_GET as $key => $val) { 
  15. if (is_numeric($val)) { 
  16.     $this->get[$key] = $this->getInt($val); 
  17. else { 
  18.     $this->get[$key] = $this->getStr($val); 
  19.     } 
  20. if (!emptyempty($_POST)) { 
  21.     foreach ($_POST as $key => $val) { 
  22. if (is_numeric($val)) { 
  23.     $this->post[$key] = $this->getInt($val); 
  24. else { 
  25.     $this->post[$key] = $this->getStr($val); 
  26.     } 
  27.     } 
  28.  
  29.     public function getInt($number
  30.     { 
  31. return intval($number); 
  32.     } 
  33.  
  34.     public function getStr($string
  35.     { 
  36. if (!get_magic_quotes_gpc()) { 
  37.     $string = addslashes($string); 
  38. return $string
  39.     } 
  40.  
  41.     public function checkInject($string
  42.     { 
  43. return eregi('select|insert|update|delete|/*|*|../|./|union|into|load_file|outfile'$string); 
  44.     } 
  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. }//開源代碼Vevb.com 
  53. return $id
  54.     } 
  55. ?> 

例子二,代碼如下:

  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. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五华县| 若羌县| 富源县| 六枝特区| 锦屏县| 甘洛县| 陇南市| 荥经县| 安岳县| 沾化县| 朝阳市| 日照市| 明溪县| 陆丰市| 乌海市| 万盛区| 进贤县| 岗巴县| 交城县| 利津县| 滕州市| 许昌市| 辽源市| 台东县| 沧州市| 银川市| 南宁市| 利辛县| 达拉特旗| 天津市| 上蔡县| 成安县| 井冈山市| 承德市| 泰顺县| 广饶县| 河间市| 策勒县| 永和县| 乌拉特中旗| 林口县|