【问题标题】:Good practice escaping multiple strings转义多个字符串的好习惯
【发布时间】:2012-05-23 21:08:50
【问题描述】:

在一次转义多个字符串(来自 POST 或 GET 的字符串)时,这是一种好的做法吗?

还有什么我应该考虑或做不同的事情吗?我现在有 PDO,但在这段代码中我不想使用它。

class Bo extends Db
{
    function clean($cThis)
    {
        $res = array();
        foreach (array_keys($cThis) as $key) {
            $res[$key] = mysql_real_escape_string(addslashes($cThis[$key]));
        }
        return $res;
    }

    function add($info)
    {
        $this->dbConnect();
        $cInfo = $this->clean($info);

        mysql_query("INSERT INTO table (b, c) VALUES ('".$cInfo['b']."', '".$cInfo['c']."')");
        $this->dbDisconnect();
    }
}

【问题讨论】:

  • 请停止使用古老的mysql_* 函数编写新代码。它们不再维护,社区已经开始 deprecation process 。相反,您应该了解prepared statements 并使用PDOMySQLi。如果您无法决定,this article 将帮助您选择。如果你想学习,here is a good PDO-related tutorial。为@alnitak +1 回答。
  • 您不想使用addslashes()mysql_real_escape_string()。删除addslashes()。另外,确保magic_quotes_gpc 没有打开。
  • @vascowhite FWIW,我总是会选择 PDO 而不是 MySQLi,这样我就不会受限于使用 MySQL 作为后端。
  • @Alnitak 我同意,但我不想对此过于虔诚 :)
  • @MarcusAdams 感谢您的回答!好的,但是当查看删除 addlashes() 后已插入数据库的内容时,此字符串:'bobo' 输出为所述 ('bobo') 而不是 \'bobo\'。但这没关系,不要乱用 mysql_query 语句? (只是仔细检查)

标签: php mysql string escaping


【解决方案1】:

好的做法是使用PDOprepared statements手动转义字符串并将它们连接到查询中,例如:

$db = DB::connect($dsn);
$sth = $db->prepare("INSERT INTO table (b, c) VALUES (?, ?)");
$res = $sth->execute(array($info['b'], $info['c']));
$db->disconnect()

哦,确保 gpc_magic_quotes 也被禁用!

【讨论】:

  • 是的,PDO 总是好的。但是,如果我们认为 PDO 不存在。或者也许这样看。有什么东西会漏掉我的支票吗?就像我忘记的东西(关于清洁功能)?
  • @Robert 不,直接入狱,不要通过 go 等等。或者,换句话说 - 不要永远,永远,永远使用原始 @ 987654325@ 功能。您应该假设 那些 不存在,因为痛苦和折磨,SQL 注入攻击将跟随所有不存在的那些。
  • 感谢您的回答。如果我这样说。我即将工作的公司想了解我如何在不使用 PDO 并且只使用 mysql_* 的情况下转义字符串。他们当然不是高科技。这是他们想要的方式,我对此无话可说。照这样说。这是一种好的(与没有 PDO 的情况一样好)的处理方式吗?
  • @Robert 告诉他们 mysql_ 函数不再是最佳实践,您将使用准备好的语句。
  • @Robert 恐怕你不会得到优秀程序员的帮助来开发糟糕的代码。
【解决方案2】:

PDO 将是最佳选择,但以防万一您没有它。有一些方法可以模仿 mysql 的 pg_query_params。写成类的方法,$this->dbconnmysqli_init返回的连接资源。当然,您可以使用适当的 mysql 类似物更改 mysqli 方法。

/**
 * Returns the single value of the array mysqli_query_params__parameters
 *
 * @param $at the position of the parameter inside the array mysqli_query_params__parameters
 * @return mixed
 */
public function mysqli_query_params__callback( $at )
{
    return $this->mysqli_query_params__parameters[ $at[1]-1 ];
}

/**
 * Parameterised query implementation for MySQL (similar PostgreSQL's PHP function pg_query_params)
 * Example: mysqli_query_params( "SELECT * FROM my_table WHERE col1=$1 AND col2=$2", array( 42, "It's ok" ), $dbconn );
 *
 * @param $query, $parameters, $datadase
 * @return mixed(resorce, false)
 * @access public
 */
public function mysqli_query_params( $query, $parameters=array(), $database=false )
{
    if( !is_array($parameters) ){
        return false;
    } else {
        if($this->is_assoc($parameters)){
            $parameters = array_values($parameters);
        }
    }

    // Escape parameters as required & build parameters for callback function
    foreach( $parameters as $k=>$v )
    {
        $parameters[$k] = ( is_int( $v ) ? $v : ( NULL===$v ? 'NULL' : "'".mysqli_real_escape_string( $this->dbconn, $v )."'" ) );
    }
    $this->mysqli_query_params__parameters = $parameters;

    // Call using mysqli_query
    if( false === $database )
    {
        $query = preg_replace_callback( '/\$([0-9]+)/', array($this, 'mysqli_query_params__callback'), $query );
        $result = mysqli_query( $this->dbconn, $query );

        if( false === $result )
        {
            $err_msg = mysqli_error($this->dbconn);
            return false;
        } else {
            return $result;
        }
    }
    else
    {
        $query = preg_replace_callback( '/\$([0-9]+)/', array($this, 'mysqli_query_params__callback'), $query );
        $result = mysqli_query( $this->dbconn, $query, $database );

        if( false === $result )
        {
            $err_msg = mysqli_error($this->dbconn);

            return false;
        } else {
            return $result;
        }
    }

    return false;
}

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    相关资源
    最近更新 更多