php对表单提交的字符串过滤处理

来源:互联网 发布:地理探测器软件下载 编辑:程序博客网 时间:2024/05/09 05:16

[php]

//过滤参数

function addslashes_deep($value){
    if (empty($value)){
        return $value;
    }else{
        return is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
    }
}

//调用的地方

if (!get_magic_quotes_gpc()){
      if (!empty($_GET)){
          $_GET  = addslashes_deep($_GET);
      }
      if (!empty($_POST)){
          $_POST = addslashes_deep($_POST);
      }
      $_COOKIE   = addslashes_deep($_COOKIE);
      $_REQUEST  = addslashes_deep($_REQUEST);
  }
[/php]

这个是防止magic_quotes_gpc 未开启导致的安全问题


另一种方法:

如果get_magic_quotes_gpc()就先stripslashes转化回来,再mysql_real_escape_string过滤。

foreach ($_COOKIE as $key => $value) {  
    if(get_magic_quotes_gpc()) $_COOKIE[$key]=stripslashes($value);  
    $_COOKIE[$key] = mysql_real_escape_string($value);  
  }


原理:是数字就按数字处理,有格式就按照格式处理,纯字符串该转义的转义,如果用户提交的数据要显示在页面上,那么一定要过滤html标签,如果用户提交的数据是html的,那么要过滤相关的js代码iframe,script,style标签等等,过滤html元素的各种js事件属性等等

原创粉丝点击