检查密码是否与用户名在同一行中匹配

Checking if password matches in the same row as the username

我有一些 MySQL 问题并且非常困惑,因为这似乎不可能。无论如何,当我尝试登录时,当我输入正确的密码时,我被发送到屏幕,提示 username/password 不正确。

基本上,我的数据库是这样的:

USERNAME    PASSWORD       EMAIL
Xp10d3      Password12345  xp10d3@gmail.com
IiBlurBeriI Password33333  iiblurberii@gmail.com

我想检查用户是否输入了他们的用户名以及密码是否匹配,但我不知道该怎么做。我在编写代码时得到了一些帮助,但它似乎不起作用,因为在验证之前可以输入任何用户名(在用户的电子邮件中验证),一旦他们输入它,密码就不必匹配。 前任。 我想要的是: 用户输入用户名 Xp10d3。用户输入密码 Password1111。有一条错误消息说密码不匹配。然后用户输入密码 Password12345。它是有效的并将 $_SESSION 变量设置为其用户名。 现在发生了什么: 用户输入数据库中的 Xp10d3。他们输入的密码是 Password12345,这是正确的,他们被发送到屏幕上,提示他们的密码不正确。

我做了一些测试并记录了所有变量。这一切都归结为 $row["PASSWORD"] 不正确的事实。我该如何解决这个问题?

一些重要说明: 变量 $_SESSION['password_not'] 不是散列密码。但是,除该变量和 $password 变量外,我得到的所有其他变量都经过哈希处理。

未散列的变量列表:

$_SESSION['password_not']
$password

散列的变量:

$row["PASSWORD"]
Any passwords in the database

代码: login_check_update.php:

<!DOCTYPE HTML>
<html>
<head>
    <style>
        body {
            text-align: center;
            font-family: sans-serif;
        }
        a {
            text-decoration: none;
            color: blue;
        }
        #logout {
            margin: 0 auto;
            text-align: center;
            border: 1px solid;
            border-radius: 5px;
            max-width:1024px;;
            height: 800px;
        }
    </style>
</head>
<body>
    <div id="logout">
        <?php
            session_start();
            /* Sends an email to the user and adds the special key to another database */
            $username = $_GET['username']; /* Gets the username that was submitted in the HTML form. */
            $password = $_GET['password']; /* Gets the password that was submitted in the HTML form. */
            $servername = "localhost"; /* MySQL database. Change if needed! Most of the time its not localhost unless you're hosting on your computer. */
            $user = 'xxx'; /* MySQL username. Change if needed. */
            $pass = 'xxx'; /* MySQL password. Change if needed. */
            $dbname = 'vibemcform'; /* MySQL database name. Change if needed. */

            $bytes = random_bytes(10); /* Randomized code */
            $key = bin2hex($bytes); /* Makes the randomized code */

            $_SESSION['username'] = $username;
            $_SESSION['password'] = $password;

            $link = "live.php";

            $con = new mysqli($servername, $user, $pass, $dbname); /* Connects to the database */
            $query = "SELECT USERNAME FROM data WHERE USERNAME = '".$username."'";
            $result = $con->query($query);
            $row = $result->fetch_assoc();
            $selectTwo = "SELECT PASSWORD FROM data WHERE PASSWORD = '".$password."'";
            $result2 = $con->query($selectTwo);
            $row2 = $result->fetch_assoc();

            /* Delete after */
            $test = $row["USERNAME"];
            $testTwo = password_verify($_SESSION['password_not'], $row["PASSWORD"]);

            if ($username == $row["USERNAME"] && password_verify($_SESSION['password_not'], $row["PASSWORD"])) {
                    echo "Found data in the database! Visit the chat!";
                    echo "<form action='live.php' method='post'><a href='".$link."'><input type='submit' name='btn1' value='$username'/></a></form>";
                    echo "Session ID: ". session_id() . ". ";
            } else {
                echo "Username not found/password incorrect. Please try again!";
            }

            $conn = null;
            exit;
        ?>
        <a href="index.html">Home</a>
    </div>
</body>
</html>

这只是一个愚蠢的错误。我的代码不起作用的原因是因为 PASSWORD 列只有 60 个字符,并且由于我使用的是 password_hash() 数据库至少需要 255 个字符,因此切断了一半以上的密码。