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

查看:16
本文介绍了在 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

推荐答案

好的,我在这个相关问题中找到了我的问题的答案:PDO 准备好的语句是否足以防止 SQL 注入?

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

感谢 Haim 将这个 Q 指向我.

Thanks to Haim for pointing this Q to me.

在非技术术语中,以下是准备好的语句如何防止注入:

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

当查询发送到数据库时,它通常作为字符串发送.数据库引擎将尝试解析字符串并将数据与指令分开,依赖于引号和语法.因此,如果您发送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"之类的内容.数据库引擎将无法解析this 并将上述内容作为指令而不是无意义的字符串.

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天全站免登陆