反sql注入功能

时间:2013-04-07 10:41:28

标签: php sql-server sql-injection

我正在使用此函数来阻止sql注入:

function filter($input)
{
  if(strpos(str_replace("''","","$input"),"'") != false)
  {
    return str_replace("'", "''", $input);
  }
  return $input;
}

使用它是否安全?谁可以以某种方式绕过它?如果可以绕过它,请给我一个关于如何保护此功能的提示或关于如何看到绕过它的示例

更新:它在SQL Server上使用

3 个答案:

答案 0 :(得分:2)

如果您不是问题领域的专家,请不要发明自己的安全解决方案。 在这里查看http://php.net/manual/en/security.database.sql-injection.php以了解有关SQL注入的更多信息。

同时使用http://www.php.net/manual/en/book.pdo.phphttp://www.php.net/manual/en/pdo.prepare.php

进行适当的预防

如果工具已经存在,则无需创建自己的工具。

答案 1 :(得分:0)

  

使用它是否安全?

NO

  

有人能以某种方式绕过它吗?

  1. 任何方式都不应该输入过滤。但是SQL查询的数据格式化。
  2. 至少它必须是

    function SQLstrFormat($str)
    {
        return "'".str_replace("'", "''", $str)."'";
    }
    
  3. 这种方式在适用的情况下是安全的。

答案 2 :(得分:0)

    function mysql_pre($value) {

            $magic_quotes_active = get_magic_quotes_gpc();
            $new_enough_php =      function_exists("mysql_real_escape_string(unescaped_string)"); //i.e. PHP >= v4.3.0

            if($new_enough_php) { //PHP v4.3.0 or higher
                //undo any magic quote effect so mysql_real_escape_string can do the work

                if($magic_quotes_active) {$value = stripslashes($value);}

                $value = mysql_real_escape_string($value);

            } else { //before PHP v4.3.0

                if(!$magic_quotes_active) {
                    $value = addslashes($value);
                }
            }
            return $value;
        }
相关问题