当前位置:Gxlcms > PHP教程 > 简明易懂的phpsql防注入代码

简明易懂的phpsql防注入代码

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

  1. //要过滤的非法字符
  2. $ArrFiltrate=array(”‘”,”;”,”union”);
  3. //出错后要跳转的url,不填则默认前一页
  4. $StrGoUrl=””;
  5. //是否存在数组中的值
  6. function FunStringExist($StrFiltrate,$ArrFiltrate){
  7. foreach ($ArrFiltrate as $key=>$value){
  8. if (eregi($value,$StrFiltrate)){
  9. return true;
  10. }
  11. }
  12. return false;
  13. }
  14. //合并$_POST 和 $_GET
  15. if(function_exists(array_merge)){
  16. $ArrPostAndGet=array_merge($HTTP_POST_VARS,$HTTP_GET_VARS);
  17. }else{
  18. foreach($HTTP_POST_VARS as $key=>$value){
  19. $ArrPostAndGet[]=$value;
  20. }
  21. foreach($HTTP_GET_VARS as $key=>$value){
  22. $ArrPostAndGet[]=$value;
  23. }
  24. }
  25. //验证开始
  26. foreach($ArrPostAndGet as $key=>$value){
  27. if (FunStringExist($value,$ArrFiltrate)){
  28. echo “”;
  29. if (emptyempty($StrGoUrl)){
  30. echo “”;
  31. }else{
  32. echo “”;
  33. }
  34. exit;
  35. }
  36. }
  37. ?>

方法二

  1. /* 过滤所有GET过来变量 */
  2. foreach ($_GET as $get_key=>$get_var)
  3. {
  4. if (is_numeric($get_var)) {
  5. $get[strtolower($get_key)] = get_int($get_var);
  6. } else {
  7. $get[strtolower($get_key)] = get_str($get_var);
  8. }
  9. }
  10. /* 过滤所有POST过来的变量 */
  11. foreach ($_POST as $post_key=>$post_var)
  12. {
  13. if (is_numeric($post_var)) {
  14. $post[strtolower($post_key)] = get_int($post_var);
  15. } else {
  16. $post[strtolower($post_key)] = get_str($post_var);
  17. }
  18. }
  19. /* 过滤函数 */
  20. //整型过滤函数
  21. function get_int($number)
  22. {
  23. return intval($number);
  24. }
  25. //字符串型过滤函数
  26. function get_str($string)
  27. {
  28. if (!get_magic_quotes_gpc()) {
  29. return addslashes($string);
  30. }
  31. return $string;
  32. }
  33. ?>

人气教程排行