SQL 注入预防与 if/else


SQL-Injection prevention with if/else

我只是考虑了我的php脚本的安全性,并阅读了很多关于sql注入的信息。现在我有一个问题要把我从中拯救出来。当我的脚本不接受像 ';" 这样的字符时,它是否正确?或者我只是从我的安卓应用程序中捕捉到这个?所以我只是在这些漫画中看到问题。我说的对吗?还是有我没有看到的?如果不是,最简单的安全方法是什么?

永远不要检查要注入的字符串,您需要做的就是不连接变量。

  1. 使用预准备语句。

Mysqli 摘录中的"示例"来自 http://www.wikihow.com/Prevent-SQL-Injection-in-PHP

$name = $_GET['username'];
if ($stmt = $mysqli->prepare("SELECT password FROM tbl_users WHERE name=?")) {
    // Bind a variable to the parameter as a string. 
    $stmt->bind_param("s", $name);
    // Execute the statement.
    $stmt->execute();
    // Get the variables from the query.
    $stmt->bind_result($pass);
   // Fetch the data.
   $stmt->fetch();
   // Display the data.
   printf("Password for user %s is %s'n", $name, $pass);
   // Close the prepared statement.
   $stmt->close();
}

阅读: http://www.veracode.com/security/sql-injection


如果您的想法是检查 ' 或 " 或 ; 的变量,这是一项艰巨的无休止的工作,最终可以使用 PDO 语句来解决。