防止SQL / XSS攻击的类?

时间:2011-07-03 01:59:48

标签: php xss sql-injection

我正在构建一个用于管理创意组合的MVC应用程序(将其放在git hub上)。我需要一些东西来保护数据库连接,基本上我有一个类来管理所有数据库事务。

我需要创建一个类或者找一个可以保护所有SQL查询不受XXS或SQL攻击的类。您对保护PHP数据库连接有什么建议?

2 个答案:

答案 0 :(得分:3)

使用PDO准备好的语句访问数据库可以使查询免于注入。 http://us2.php.net/manual/en/pdo.prepare.php

使用htmlspecialchars()使输出免受xxs的影响。 http://us2.php.net/manual/en/function.htmlspecialchars.php

答案 1 :(得分:-1)

尝试使用此功能过滤POST,GET请求

function protect($string) 
 { 
      if (ini_get('magic_quotes_gpc') == 'off') // check if magic_quotes_gpc is on and if not add slashes
            { 
             $string = addslashes($string); 
            }  
// move html tages from inputs
$string = htmlentities($string, ENT_QUOTES);
//removing most known vulnerable words
$codes = array("script","java","applet","iframe","meta","object","html", "<", ">", ";", "'","%");
$string = str_replace($codes,"",$string);
//return clean string
return $string; 
}

您可以使用array_map函数

轻松地将其应用于整个输入
$input = array_map('protect','$_POST');
$input = array_map('protect','$_GET');
相关问题