Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查“密码验证”的返回值时出现问题_Php_Sql_Mysqli_Passwords - Fatal编程技术网

Php 检查“密码验证”的返回值时出现问题

Php 检查“密码验证”的返回值时出现问题,php,sql,mysqli,passwords,Php,Sql,Mysqli,Passwords,我的密码验证有问题。即使我知道密码输入正确,我仍然得到一个错误的结果。我使用以下代码在mySql数据库中输入哈希值:我已经替换了服务器登录详细信息;输入来自用户扫描NFC微芯片时创建的表单: $servername = "localhost"; $username = "xxxxxxxxx"; $password = "xxxxxxxxx"; $database = "xxxxxxxxx"; // substitute your mysql database name $hash = passw

我的密码验证有问题。即使我知道密码输入正确,我仍然得到一个错误的结果。我使用以下代码在mySql数据库中输入哈希值:我已经替换了服务器登录详细信息;输入来自用户扫描NFC微芯片时创建的表单:

$servername = "localhost";
$username = "xxxxxxxxx";
$password = "xxxxxxxxx";
$database = "xxxxxxxxx"; // substitute your mysql database name
$hash = password_hash($Pass, PASSWORD_DEFAULT);
if ($UID === "0") {
    echo "You have not scanned a chip to enter the registration process";
} else {
    $Type = $_POST["Type"];
    $Units = $_POST["UNITS"];
    $LstStln = $_POST["LstStln"];
    $Country = $_POST["Country"];
    if (empty($_POST["eMail"])) {
        $emailErr = "Email is required";
        echo "Email is required";
    } else {
        $eMail = test_input($_POST["eMail"]);
        // check if e-mail address is well-formed
        if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $eMail)) {
            die("Invalid email format. Try again.");
        }
        // Create connection
        $conn = new mysqli($servername, $username, $password, $database);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        $sql = "INSERT INTO ItsMine (UID, Password, email, Type, UNITS, LstStln, Country) VALUES ('$UID', '$hash', '$eMail', '$Type', '$Units', '$LstStln', '$Country')";
        $result = $conn->query($sql);
    }
}
这是处理此表单输入的相应代码,密码验证返回false:

$servername = "localhost";
$username = "xxxxxxxxx";
$password = "xxxxxxxxx";
$database = "xxxxxxxxxx"; // substitute your mysql database name
$email = $_POST['email'];
$Pass = $_POST['Password'];
// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
//get the hashed password from the database
$sql = "SELECT * From ItsMine where eMail = '$email'";
$result = $conn->query($sql);
$row = mysqli_fetch_assoc($result);
$hash = $row["Password"];
//Check password entered against the stored hash
if (password_verify($Pass, $hash)) {
    $tql = "SELECT * From ItsMine where eMail = '$email'";
}

在你的问题中没有足够的信息来确定你的问题,但我要做一个有根据的猜测,并说你的问题在于学校。请,如果你曾经跟随他们的教程,停止!他们的代码经常被破坏,并提出了骇人听闻的做法

您的问题可以通过在代码中使用test_输入来解释。它什么也不做,只会损坏您的数据并导致代码出现问题。人们甚至可以认为这是一个安全漏洞。当您在代码中插入电子邮件时,我看到您使用$email=test\u input$\u POST[email];,但是,当您从数据库中选择哈希值时,您不会这样做。这很可能就是您的select无法按预期工作的原因。要调试它,您可以比较var_dumptest_input$_POST[eMail]的输出;和var_dump$_POST[eMail];。如果它们不同,则表示此函数已损坏您的数据

另一个问题是缺少您应该使用的参数化预处理语句。它们由或提供。永远不要相信任何形式的输入

您还应该启用MySQLi错误显示,以防您的SQL在某个地方出错

最后,您应该阅读另一篇有价值的文章:

为了更好地发挥影响,我将您的标题缩写,并将原始问题作为正文包含在内。使用diemysqli_error$conn是一个非常糟糕的主意;因为它可能会泄露敏感信息。更多解释请参见本文:Ok。我感谢你花时间回答我的问题和你以后的评论。我发现这里的整个经历都是负面的,因为我的问题在以后并不流行。然而,鉴于你的评论,我决定从头开始,并尝试使用准备好的声明正确地完成这项工作。祝我好运,谢谢你的反馈。