• <bdo id='ETdP8'></bdo><ul id='ETdP8'></ul>
  • <i id='ETdP8'><tr id='ETdP8'><dt id='ETdP8'><q id='ETdP8'><span id='ETdP8'><b id='ETdP8'><form id='ETdP8'><ins id='ETdP8'></ins><ul id='ETdP8'></ul><sub id='ETdP8'></sub></form><legend id='ETdP8'></legend><bdo id='ETdP8'><pre id='ETdP8'><center id='ETdP8'></center></pre></bdo></b><th id='ETdP8'></th></span></q></dt></tr></i><div id='ETdP8'><tfoot id='ETdP8'></tfoot><dl id='ETdP8'><fieldset id='ETdP8'></fieldset></dl></div>

    <small id='ETdP8'></small><noframes id='ETdP8'>

  • <legend id='ETdP8'><style id='ETdP8'><dir id='ETdP8'><q id='ETdP8'></q></dir></style></legend>

      1. <tfoot id='ETdP8'></tfoot>
      2. 在 PHP 中,PDO 如何防止 SQL 注入?准备好的语句如

        时间:2023-10-05
        1. <tfoot id='SRpaX'></tfoot>

            <small id='SRpaX'></small><noframes id='SRpaX'>

            <i id='SRpaX'><tr id='SRpaX'><dt id='SRpaX'><q id='SRpaX'><span id='SRpaX'><b id='SRpaX'><form id='SRpaX'><ins id='SRpaX'></ins><ul id='SRpaX'></ul><sub id='SRpaX'></sub></form><legend id='SRpaX'></legend><bdo id='SRpaX'><pre id='SRpaX'><center id='SRpaX'></center></pre></bdo></b><th id='SRpaX'></th></span></q></dt></tr></i><div id='SRpaX'><tfoot id='SRpaX'></tfoot><dl id='SRpaX'><fieldset id='SRpaX'></fieldset></dl></div>
              <tbody id='SRpaX'></tbody>

              <legend id='SRpaX'><style id='SRpaX'><dir id='SRpaX'><q id='SRpaX'></q></dir></style></legend>
              • <bdo id='SRpaX'></bdo><ul id='SRpaX'></ul>

                  本文介绍了在 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 注入?准备好的语句如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PDO::PARAM_INT 在 bindParam 中很重要吗? 下一篇:在 PDO 中强类型参数的原因?

                  相关文章

                  最新文章

                    <legend id='2tFV2'><style id='2tFV2'><dir id='2tFV2'><q id='2tFV2'></q></dir></style></legend>

                      <bdo id='2tFV2'></bdo><ul id='2tFV2'></ul>

                  1. <small id='2tFV2'></small><noframes id='2tFV2'>

                  2. <tfoot id='2tFV2'></tfoot>
                    1. <i id='2tFV2'><tr id='2tFV2'><dt id='2tFV2'><q id='2tFV2'><span id='2tFV2'><b id='2tFV2'><form id='2tFV2'><ins id='2tFV2'></ins><ul id='2tFV2'></ul><sub id='2tFV2'></sub></form><legend id='2tFV2'></legend><bdo id='2tFV2'><pre id='2tFV2'><center id='2tFV2'></center></pre></bdo></b><th id='2tFV2'></th></span></q></dt></tr></i><div id='2tFV2'><tfoot id='2tFV2'></tfoot><dl id='2tFV2'><fieldset id='2tFV2'></fieldset></dl></div>