简体   繁体   English

我的PDO INSERT INTO是否足够安全?

[英]Is my PDO INSERT INTO enough secure?

I would like your opinion about my code. 我希望您对我的代码有意见。 Is it secure enough against any injections. 是否足够安全以防止任何注射。 Thanks for your replies. 多谢您的回覆。

class Product extends DB {

  public function __construct() {
    $db = $this->DB();

    if(isset($_POST['productName'])) {
        foreach ($_POST as $key => $value) {
            if (ini_get('magic_quotes_gpc'))
                $_POST[$key] = stripslashes($_POST[$key]);
                $_POST[$key] = htmlspecialchars(strip_tags($_POST[$key]));
        }
        $this->AddProduct();
    }
  }

  public function AddProduct() {
    $sSQL = "INSERT INTO ".PREFIX."product (productName, productPrice) 
                VALUES (:productName,:productPrice)";
    $query = $this->db->prepare($sSQL);
    $query->execute(array(
        ":productName" => $_POST['productName'],
        ":productPrice" => $_POST['productPrice']
    )); 
  }
}

Using query parameters is enough to make it secure against SQL injection vulnerability. 使用查询参数足以使其免受SQL注入漏洞的攻击。

The code that calls htmlspecialchars and strip_tags is not relevant to SQL injection. 调用htmlspecialchars和strip_tags的代码与SQL注入无关。 It might be called for to prevent Cross-Site Scripting vulnerabilities, but that's a separate issue. 可能需要使用它来防止跨站点脚本漏洞,但这是一个单独的问题。 I don't recommend doing those steps as you insert data into the database. 在将数据插入数据库时​​,我不建议执行这些步骤。 Just filter against XSS vulnerabilities when you output to HTML. 当您输出到HTML时,只需过滤XSS漏洞即可。 Otherwise, you get literal & 否则,您将得到字面& sequences stored in your database, and that's premature. 序列存储在数据库中,还为时过早。 You aren't necessarily going to use the data to display in HTML every time. 您不必每次都使用数据在HTML中显示。 Just encode it when you output, not when you input. 仅在输出时对其进行编码,而不在输入时进行编码。

I never bother with compensating for the possible magic_quotes_gpc. 我从来没有为可能的magic_quotes_gpc进行补偿。 Test for it when you deploy your app, and abort the deployment. 在部署应用程序时进行测试,然后中止部署。 It's not valid for any PHP instance to set magic_quotes_gpc in 2014. 在2014年对任何 PHP实例设置magic_quotes_gpc都是无效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM