易受SQL注入攻击

时间:2019-03-20 03:50:07

标签: security sql-injection

我正在做一个项目来查找易受攻击的代码。这个代码片段容易受到SQL注入的攻击吗?

if(ISSET ($_POST['login'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
    $query = $conn->query("SELECT * FROM `admin` WHERE `username` = '$username' && `password` = '$password'") or die(mysqli_error());

1 个答案:

答案 0 :(得分:0)

是的。最好使用Mysqli的参数化查询模式。

<?php

    if (isset($_POST['user']) && isset ($_POST['pass'])) {

        $user = mysql_real_escape_string ($_POST['user']),
        $pass = mysql_real_escape_string ($_POST['pass']);
        $conn = new mysqli("$db_host", "$db_user", "$db_pass", "$db");
        $sql = "select * from admin where username=? and password=?";
        $cmd = $conn->prepare($sql);
        $cmd->bind_param("ss", $user, $pass);
        $cmd->execute();
         ......
         ......
    else {
        echo "Username and password cant not be null !";
    }
?>