/** * 递归方式的对变量中的特殊字符进行转义 * * @access public * @param mix $value * * @return mix */ function addslashes_deep($value) { if (empty($value)) { return $value; } else { return is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value); } }
上面的array_map函数对多维数组递归调用addslashes_deep来进行过滤。
/** * 递归方式的对变量中的特殊字符去除转义 * * @access public * @param mix $value * * @return mix */ function stripslashes_deep($value) { if (empty($value)) { return $value; } else { return is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); } }
/** * 自定义 header 函数,用于过滤可能出现的安全隐患 * * @param string string 内容 * * @return void **/ function ecs_header($string, $replace = true, $http_response_code = 0) { if (strpos($string, '../upgrade/index.php') === 0) { echo '<script type="text/javascript">window.location.href="' . $string . '";</script>'; } $string = str_replace(array("\r", "\n"), array('', ''), $string); if (preg_match('/^\s*location:/is', $string)) { @header($string . "\n", $replace); exit(); } if (empty($http_response_code) || PHP_VERSION < '4.3') { @header($string, $replace); } else { @header($string, $replace, $http_response_code); } }
PHP的header函数对URL中包含的数据缺少正确过滤,远程攻击者可以利用这个漏洞进行脚本注入攻击。
另外PHP的header()实现上存在潜在的缓冲区溢出漏洞。
/** * 对 MYSQL LIKE 的内容进行转义 * * @access public * @param string string 内容 * @return string */ function mysql_like_quote($str) { return strtr($str, array("\\\\" => "\\\\\\\\", '_' => '\_', '%' => '\%', "\'" => "\\\\\'")); }
另外还有htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。以及htmlentities函数