php - 如何处理 PHP 和 PDO 中的用户文本输入?

标签 php mysql pdo sql-injection

我应该使用 PDO quote() 然后在检索时从用户输入中去除引号和斜线,还是有更好的方法?

当将用户输入从 PHP 表单插入 MySQL 时,任何单引号(撇号)都会截断数据。我目前正在使用 PDO 准备语句和 bindValue() 语句。我不确定是否使用 PDO quote() 方法,该方法似乎将我的所有字符串都封装在引号中,然后用反斜杠(“\”)转义字符串中的引号。据我所知,如果我使用这种方法,那么我需要在检索数据时从字符串中去除引号。这是最好的方法吗?使用 PDO quote() 然后使用 substring 删除封装单引号是否安全?

此外,我有点困惑为什么单引号会截断数据输入。我认为 PDO bindValue() 应该为我转义单引号。我可能弄错了。手册不是很详细。

我检查过的东西:

  1. PHP 魔术引号不在我的 php.ini 文件中,因为我使用的是一个已贬值的版本。​​
  2. 阅读 PDO 的所有手册(bindParam()bindValue()prepare()quote())
  3. 阅读 StackOverflow 上的所有类似问题。

这是我正在使用的 PDO 插入代码:

//create query string
$profile_update_query_text = "
UPDATE user_profile 
SET public=:public, headline=:headline, display_name=:display_name, skype=:skype, description=:description
WHERE user_id=:user_id";

//update table is required to modify existing user profile data
$query_profile_update_insert = $this->db_connection->prepare($profile_update_query_text);
$query_profile_update_insert->bindValue(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$query_profile_update_insert->bindValue(':public', $public, PDO::PARAM_INT);
$query_profile_update_insert->bindValue(':headline', $headline, PDO::PARAM_STR);
$query_profile_update_insert->bindValue(':display_name', $display_name, PDO::PARAM_STR);
$query_profile_update_insert->bindValue(':skype', $skype, PDO::PARAM_STR);
$query_profile_update_insert->bindValue(':description', $description, PDO::PARAM_STR);

//execute profile insert 
$query_profile_update_insert->execute();

我还包含了用于创建 PDO 连接的函数,因此可以验证我没有使用任何会导致问题的设置:

private function databaseConnection()
    {
        // connection already opened
        if ($this->db_connection != null) {
            return true;
        } else {
        // create a database connection, using the constants from config/config.php
        try {
            $this->db_connection = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME . ';charset=utf8', DB_USER, DB_PASS);
            return true;
        // If an error is catched, database connection failed
        } catch (PDOException $e) {
            $this->errors[] = MESSAGE_DATABASE_ERROR;
            return false;
        }
    }
}

如果用户输入如下标题:

I'm a great skiier

在 MySQL 中,我要么得到:

I or 'I\'m a great skiier'

取决于我是否使用 PDO quote()

PDO bindParam() 的运行方式是否存在问题,它应该转义单引号,或者是否有处理此问题的首选方法?

编辑——我使用以下方法检查是否启用了魔术引号:

if(get_magic_quotes_gpc()){
    echo "magic quotes on";
}else{
    echo "magic quotes off";
}

最佳答案

为此,您永远不需要将 PDO quote() 与 MySQL 一起使用。它只有在需要将 SQL 插入到语句中时才有用,而 bindParam()/bindValue() 无法做到这一点。关于为什么引号导致截断,我有一种感觉,这里发生了其他事情。 bindParam()/bindValue() 应该正确转义您绑定(bind)的任何字符串,而您根本不必修改或过滤它。

关于php - 如何处理 PHP 和 PDO 中的用户文本输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31366335/

相关文章:

PHP 子类不会覆盖新值

PHP/FPDF : How to insert image at cell?

mysql - 如何将我网站上的直接借记信息传递给直接借记提供商

php - 如何使用 PDO 准备语句执行 SELECT LIKE - 值对象在这里有用吗?

php - 这段代码有什么问题?

mysql - nodejs X DevApi 添加返回 id

mysql - Excel导入数据报错1064,保留字符

php - Number_format PDO 结果

mysql - 多个插入并保持 PDO 准备语句的安全性

php - 将 PHP 对象序列化为 JSON