在PHP中,PDO如何防止SQL注入?准备好的语句如何工作? [英] In PHP, how does PDO protect from SQL injections? How do prepared statements work?

查看:86
本文介绍了在PHP中,PDO如何防止SQL注入?准备好的语句如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解使用预备语句来保护数据库免受SQL注入的正确方法.我想了解如何准备好的语句保护我的数据库.

I understand the right way to protect a db from SQL injection is by using prepared statements. I would like to understand how prepared statements protect my db.

对于初学者来说,准备好的语句是否与参数化查询"一样?

For starters, are prepared statements the same thing as "parameterised queries"?

作为一个示例,我将下面的代码粘贴到在用户表中插入新用户的位置.这样安全吗? PDO如何工作以使其安全?需要做更多的事情来保护数据库免受注入吗?

As an example, I'm pasting below my code for the insertion of a new user in a user table. Is that secure? How does PDO work to make it secure? Does anything more needs to be done to secure the db from injection?

在"Class_DB.php"中:

In 'Class_DB.php':

class DB {
 private $dbHost;
 private $dbName;
 private $dbUser;
 private $dbPassword;   
 function __construct($dbHost, $dbName, $dbUser, $dbPassword) {
  $this->dbHost=$dbHost;
  $this->dbName=$dbName;
  $this->dbUser=$dbUser;
  $this->dbPassword=$dbPassword;
 }
 function createConnexion() {
  return new PDO("mysql:host=$this->dbHost;dbName=$this->dbName", $this->dbUser, $this->dbPassword);
 }
}

在"DAO_User.php"中:

In 'DAO_User.php':

require_once('Class_DB.php');

class DAO_User {
 private $dbInstance;
 function __construct($dbInstance){
  $this->dbInstance=$dbInstance;
 }
 function createUser($user){
  $dbConnection=$this->dbInstance->createConnexion();
  $query=$dbConnection->prepare("INSERT INTO users (userName, hashedPassword, userEmail) VALUES (?,?,?)");
  $query->bindValue(1, $user->userName);
  $query->bindValue(2, $user->hashedPassword);
  $query->bindValue(3, $user->userEmail);
  $query->execute();
 }
}

谢谢

JDelage

推荐答案

好,我在以下相关问题中找到了我的问题的答案:

Ok, I found the answer to my question in this related question: Are PDO prepared statements sufficient to prevent SQL injection?

感谢Haim向我指出这个问题.

Thanks to Haim for pointing this Q to me.

用非技术术语来说,这是准备好的语句如何防止注入:

In non technical terms, here is how prepared statements protect from injection:

查询发送到数据库时,通常以字符串形式发送. db引擎将尝试使用引号和语法来解析字符串并将数据与指令分开.因此,如果您发送"SELECT * WHERE'用户提交的数据'EQUALS'表行名',则引擎将能够解析指令.

When a query is sent to a data base, it's typically sent as a string. The db engine will try to parse the string and separate the data from the instructions, relying on quote marks and syntax. So if you send "SELECT * WHERE 'user submitted data' EQUALS 'table row name', the engine will be able to parse the instruction.

如果您允许用户输入将在用户提交的数据"中发送的内容,那么他​​们可以在其中包含"..."或如果IF 1 = 1 ERASE DATABASE"之类的东西.db引擎将无法解析这将把上面的内容作为指示,而不是没有意义的字符串.

If you allow a user to enter what will be sent inside 'user submitted data', then they can include in this something like '..."OR IF 1=1 ERASE DATABASE'. The db engine will have trouble parsing this and will take the above as an instruction rather than a meaningless string.

PDO的工作方式是,它分别发送指令(prepare("INSERT INTO ...)")和数据.数据是分别发送的,清楚地理解为仅是数据和数据.db引擎不是甚至尝试分析数据字符串的内容以查看它是否包含指令,并且不考虑任何潜在的破坏性代码片段.

The way PDO works is that it sends separately the instruction (prepare("INSERT INTO ...)) and the data. The data is sent separately, clearly understood as being data and data only. The db engine doesn't even try to analyze the content of the data string to see if it contains instructions, and any potentially damaging code snipet is not considered.

这篇关于在PHP中,PDO如何防止SQL注入?准备好的语句如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆