当前位置:首页 > PHP教程 > PHP高级教程

php快速查找数据库中恶意代码的方法

本文实例讲述了php快速查找数据库中恶意代码的方法。分享给大家供大家参考。具体如下:

数据库被输入恶意代码,为了保证你的数据库的安全,你必须得小心去清理。有了下面一个超级方便的功能,即可快速清除数据库恶意代码。

function cleaninput($input) {
 $search = array(
  '@]*?>.*?@si', // strip out javascript
  '@<[/!]*?[^<>]*?>@si', // strip out html tags
  '@
]*?>.*?
@siu', // strip style tags properly
  '@@' // strip multi-line comments
 );
  $output = preg_replace($search, '', $input);
  return $output;
 }
function sanitize($input) {
  if (is_array($input)) {
    foreach($input as $var=>$val) {
      $output[$var] = sanitize($val);
    }
  }
  else {
    if (get_magic_quotes_gpc()) {
      $input = stripslashes($input);
    }
    $input = cleaninput($input);
    $output = mysql_real_escape_string($input);
  }
  return $output;
}
// usage:
$bad_string = "hi! it's a good day!";
$good_string = sanitize($bad_string);
// $good_string returns "hi! it's a good day!"
// also use for getting post/get variables
$_post = sanitize($_post);
$_get = sanitize($_get);

希望本文所述对大家的php程序设计有所帮助。


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!