問題描述
我了解保護數據庫免受 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 注入?準備好的語句如何工作?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!