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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

PHP操作Redis常用技巧总结

本文实例讲述了PHP操作Redis常用技巧。分享给大家供大家参考,具体如下: 一、Redis连接与认证 //连接参数:ip、端口、连接超时时间,连接成功返回true,否则返回fals...

给PHP开发者的编程指南 第一部分降低复杂程度

PHP 是一门自由度很高的编程语言。它是动态语言,对程序员有很大的宽容度。作为 PHP 程序员,要想让你的代码更有效,需要了解不少的规范。很多年来,我读过很多编程方面的书籍,与很多资深程...

利用php生成验证码

话不多说,请看代码: <?php /** * php生成验证码 * @param $width 画布宽 * @param $height 画布高 * @par...

PHP 验证码的实现代码

checkcode.php 生成验证码图片,还有变量 $_SESSION[check_pic]。 复制代码 代码如下: <? session_start(); for($i=0;...

php str_pad 函数使用详解

string str_pad ( string , int pad_length , string pad_string , int pad_type); string 指定字符串,pa...