【问题标题】:Is this query secure from sql injection? [duplicate]这个查询对 sql 注入安全吗? [复制]
【发布时间】:2016-08-04 19:44:29
【问题描述】:

这个查询对 sql 注入安全吗?

并且必须在每个函数中编写 $connection->close();?

function insert_mytable() {
    global $connection; 

    $text = 'bla bla';
    $hashtag = 'bla bla';

    // Prepare the statement
    $stmt = $connection->prepare("INSERT INTO my_table (text, hashtag) VALUES (?, ?)");
    $stmt->bind_param('ss', $text, $hashtag);

    // Execute the statement
    $stmt->execute();

    // Close the statement
    $stmt->close();
    $connection->close();
}

【问题讨论】:

  • 不需要关闭连接,如果您在每次查询后都这样做会适得其反。
  • 顺便说一句...$connection 未定义。

标签: php mysql mysqli


【解决方案1】:

回答您的第一个问题:是*

*由于您使用的是准备好的语句,因此您的代码在大多数情况下不会受到 SQL 注入攻击。出于您的目的,上述代码很可能是您针对 SQL 注入所需的全部防御措施;但是,如果您想知道在什么情况下准备好的陈述是不够的,您应该查看this question 的第一个答案,因为 Joel Coehoorn 解释得比我好得多。

至于您的第二个问题,正如 ac.freelancer 和 Charlotte Dunois 已经指出的那样,不需要关闭连接,尤其是如果您打算再次使用该连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 2013-03-17
    • 2016-11-11
    • 1970-01-01
    • 2013-03-07
    相关资源
    最近更新 更多