这个 PDO 函数可以安全地避免 sql 注入吗
我有以下插入功能。 sql注入是否安全?如果不是那么我该如何保证它的安全。
public function insert($postValues, $table){
$dbh = $this->connect();
try {
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$fields = implode(array_keys($postValues), ',');
$values = "'".implode(array_values($postValues), "','")."'";
$insertQuery = 'INSERT INTO '.$table.' ('.$fields.') VALUES (:'.$fields.')';
$stmt = $dbh->prepare($insertQuery);
foreach($postValues as $vals) {
$stmt->execute($vals);
}
$message = $sucessMessage;
}
catch(PDOException $e){
$message = $e->getMessage();
}
$dbh = null;
return $message;
}
提前致谢
I have the following insert function. Is it safe from a sql injection. If it isn't then how do I make it safe.
public function insert($postValues, $table){
$dbh = $this->connect();
try {
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$fields = implode(array_keys($postValues), ',');
$values = "'".implode(array_values($postValues), "','")."'";
$insertQuery = 'INSERT INTO '.$table.' ('.$fields.') VALUES (:'.$fields.')';
$stmt = $dbh->prepare($insertQuery);
foreach($postValues as $vals) {
$stmt->execute($vals);
}
$message = $sucessMessage;
}
catch(PDOException $e){
$message = $e->getMessage();
}
$dbh = null;
return $message;
}
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果每个列类型都是
PDO::PARAM_STR
,那么使用 PDOStatement::execute。但是,如果列类型不同,则在使用 PDOStatement::bindParam。接受看似用户输入的表名和列名并不是一个好主意。如果表名或列名不正确,查询将会失败,但您需要非常小心,以确保表名和列名可以安全使用。以下示例在执行任何 SQL 之前根据白名单检查表名和列名:
If each column type is a
PDO::PARAM_STR
, then it is fairly simple to bind your parameters to unamed paramter markers using PDOStatement::execute. However, if the column types vary, then you need to specify the column type for each column when you bind to it with PDOStatement::bindParam.Accepting table and column names from what appears to be user input, is not a good idea. The query will fail if the table or column names are incorrect, but you need to be very careful to ensure that the table and column names are safe to use. The following example checks the table and column names against a whitelist, prior to executing any SQL:
顺便说一句:当询问 PDO 是否比其他 PHP MySQL 连接库更安全地避免 SQL 注入时,当我们谈论
PDO_MYSQL
时,答案是 NO(不知道是否以下对于其他一些数据库也是如此)。人们甚至可以反过来说,PDO 比任何其他 PHP MySQL 连接库(
ext/mysql
和ext/mysqli
)更不安全且更危险,因为PDO_MYSQL
允许在一个 SQL 语句中进行多个查询,而ext/mysql
完全停止多查询,并且ext/mysqli
有一个独立的函数mysqli_multi_query()
。我只是试图找到任何来源来支持这个声明,但我发现的唯一的东西是:
使用 PDO 和 Zend Framework 防止 SQL 注入
<块引用>
PDO_MySQL 是一个比较危险的
比任何其他传统应用程序
MySQL 应用程序。传统MySQL
只允许单个 SQL 查询。在
PDO_MySQL则没有这样的限制,
但你有被注射的风险
多个查询。
By the way: when asking if PDO is safer from sql injection than some other PHP MySQL connection library, the answer is NO when we talk about
PDO_MYSQL
(don't know if the following is true for some other databases).One could even argue the other way round, PDO is less secure and more dangerous than any other PHP MySQL connection library (
ext/mysql
andext/mysqli
) becausePDO_MYSQL
allows for multiple queries in one SQL statement whileext/mysql
stops multi-queries completely andext/mysqli
has a sparate functionmysqli_multi_query()
.I just tried to find any sources to support this statement, but the only things I found are:
Protection against SQL Injection using PDO and Zend Framework
不,因为您只是使用 PDO 扩展执行原始 SQL 查询。我做了类似以下的事情:
我确信您可以修改上面的内容以适合您的设置。
No, because you're just executing a raw SQL query with the PDO extension. I do something similar to the following:
I'm sure you can modify the above to fit your set-up.
唯一明智的方法是使用
PDO::prepare
带参数(参见手册中的示例)。此外,字段名称应取自可信来源,即不是用户。这样,您就可以从受信任的组件构建查询字符串:The only sane way is to use
PDO::prepare
with parameters (see example in manual). Moreover, field names should be taken from trusted source, i.e. not user. This way, you build your query string from trusted components: