php - 输入登录密码时,要检查的密码不==

标签 php mysql authentication md5

我有一个 PHP 登录表单,它使用 MD5 哈希来保证我的密码安全(我知道 MD5 很糟糕,我稍后会更改它;它现在只是用于开发目的)。当我在密码上使用 == 进行比较时,它不会让我登录。

md5ing 是否返回字符串或其他内容,可能是因为在我的数据库中我只有单词密码的字符串,就像它经过 MD5 处理一样(“5f4dcc3b5aa765d61d83”)?这是我登录的开始阶段,在我更好地研究它们并使其工作之后,我将添加准备语句以及所有这些。

此外,我获得的域正在运行 PHP 版本 5.3.28,因此 password_verify() 不适用于此开发站点。

索引页面(登录)

<?php
session_start();

if (isset($_POST['username'])) {

        // Include the databas connection script
    include_once("includes/connect.inc.php");

    // Set the posted data from the form into local variables
    $usname = $_POST['username'];
    $paswd  = $_POST['password'];

    $usname = mysqli_real_escape_string($connect, $usname);

    $sql          = "SELECT * FROM dealerEmployees WHERE firstName = '$usname' LIMIT 1";
    $query        = mysqli_query($connect, $sql);
    $row          = mysqli_fetch_row($query);
    $uid          = $row[0];
    $dbUsname     = $row[1];
    $firstName    = $row[1];
    $lastName     = $row[2];
    $dbPassword   = $row[3];
    $permission   = $row[4];
    $address      = $row[5];
    $email        = $row[6];
    $phone        = $row[7];
    $profilePhoto = $row[8];
    $bannerPhoto  = $row[9];

    // Check if the username and the password they entered was correct
    if ($usname == $dbUsname) {

            if($paswd == $dbPassword){
            // Set session 
            $_SESSION['id'] = $uid;
            $_SESSION['userId'] = $uid;
            $_SESSION['username'] = $usname;
            $_SESSION['firstName'] = $firstName;
            $_SESSION['lastName'] = $lastName;
            //$_SESSION['password'] = $dbPassword;
            $_SESSION['permission'] = $permission;
            $_SESSION['address'] = $address;
            $_SESSION['phone'] = $phone;
            $_SESSION['email'] = $email;
            $_SESSION['profilePhoto'] = $profilePhoto;
            $_SESSION['bannerPhoto'] = $bannerPhoto;
            // Now direct to users feed
            header("Location: hub.php");
        }
    } else {
        echo "<h2>Oops that username or password combination was incorrect.
        <br /> Please try again.</h2>";
    }

}
?>

<form id="form" action="index.php" method="post" enctype="multipart/form-data">
<input type="text" name="username" placeholder="Please Enter Your First Name"/> <br />
<input type="password" name="password" placeholder="Please Enter Your Password"/> <br />
 <button class="button" type="submit">Log In</button>

</form>

最佳答案

首先,这是一个非常糟糕的主意:

$paswd  = strip_tags($_POST['password']);

如果我小心地生成强密码,那么我的密码很可能类似于 c5<dZIuJYWUP3>y 。你刚刚把我的密码变成了非常容易破解的c5y .

使用 MD5 也是一个非常糟糕的主意。使用 password_hash password_verify 利用 PHP 对行业标准 bcrypt 算法的内置处理。 没有理由练习编写糟糕的代码。

Does md5ing return a string or something else maybe?

是的,它将返回由 0-9 和 a-f 字符组成的 32 个字符的字符串。 password 的 MD5 哈希值,例如,是5f4dcc3b5aa765d61d8327deb882cf99 .

But when I use == on the password comparing it wont log me in.

那么用户输入的密码的 MD5 哈希值与您存储在数据库中的 MD5 哈希值不匹配。

为了便于调试,请考虑手动编辑数据库行以使用哈希 5f4dcc3b5aa765d61d8327deb882cf99 ,然后尝试使用密码 password 登录。如果此方法有效,则说明您没有在数据库中存储 MD5 哈希值,或者您在数据库中存储了错误 MD5 哈希值 - 可能是因为您的 strip_tags东西。

如果没有,则说明发生了一些更基本的事情 - 可能通过使用 mysqli_fetch_assoc 更容易调试而不是mysqli_fetch_row在您的SELECT *上查询。

关于php - 输入登录密码时,要检查的密码不==,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31371218/

相关文章:

javascript - 推迟广告脚本执行

php - 如何在php中获取第一条和最后一条记录

尽管已被异常处理程序捕获,但仍显示 PhP 异常消息

mysql - 将 CAST 与 WHERE 结合使用

node.js - 如何在 Passport google auth 回调函数中获取用户个人资料数据?

javascript - onclick 按钮不执行任何操作;未调用 JavaScript 函数

php - 将信息从一个 View 传递到另一个 View

php - Paypal IPN : Update the value of a column after payment?

php - Laravel 5.3 电子邮件队列中不允许序列化 'Closure'

windows - IIS6 Windows 身份验证仅在通过 DNS 名称(而非 IP 地址)访问站点时有效