php - 如何将我插入的密码与 php 中的 md5 哈希进行比较

标签 php mysql md5

我在简单的登录程序中工作,我输入我的密码并通过 MD5 哈希加密。但现在当我必须登录时,我不能去扔那个。我无法将我的登录密码与数据库中的 md5 哈希进行比较。

这就是我所做的,我把我在评论中使用的 md5。

<?php


if(isset($_SESSION['userid']) && isset($_REQUEST['action']) && $_REQUEST['action'] == 'logout')
{
    //We log him out by deleting the username and userid sessions
    unset($_SESSION['username'], $_SESSION['userid'], $_SESSION['name']);

    $flagLogoutMessage = true;

}
// Login Form Check here
$ousername = '';
//We check if the form has been sent
if(isset($_POST['username'], $_POST['pwd']))
{
    //We remove slashes depending on the configuration
    if(get_magic_quotes_gpc())
    {
        $ousername = stripslashes($_POST['username']);
        $username = mysql_real_escape_string(stripslashes($_POST['username']));
        $password = stripslashes($_POST['pwd']);
        //$password = MD5($password);
    }
    else
    {
        $username = mysqli_real_escape_string($conn, $_POST['username']);
        $password = $_POST['pwd'];
        //$password = MD5($password);

    }
    //We get the password of the user
    $req = mysqli_query($conn, 'select pwd,rec_id from user_master where username="'.$username.'" AND u_status="Y"');
    $dn = mysqli_fetch_array($req);
    //We compare the submited password and the real one, and we check if the user exists
    if($dn['pwd']==$password and mysqli_num_rows($req)>0)
    {
        //If the password is good, we dont show the form
        $form = false;
        //We save the user name in the session username and the user Id in the session userid
        $_SESSION['username'] = $_POST['username'];
        $_SESSION['userid'] = $dn['rec_id'];

        $select_name = mysqli_query($conn, 'select f_name,l_name from user_master where rec_id="'.$dn['rec_id'].'"');
        $dn = mysqli_fetch_array($select_name);
        if(mysqli_num_rows($select_name)>0)
        {
            $_SESSION['name'] = $dn['f_name']." ".$dn['l_name'];
            // $_SESSION['last_name'] = $dn['last_name'];
        }


        $flagLoginMessage = true;

    }
    else
    {
        //Otherwise, we say the password is incorrect.
        $form = true;
        $message = 'The username or password is incorrect or Your account is blocked by Admin';
    }
}
else
{
    $form = true;
}





?>

最佳答案

Maybe can use SQL

$password = md5($_POST['pwd']);
$req = mysqli_query($conn, 'select pwd,rec_id from user_master where username="'.$username.'" and pwd="'.$password.'" AND u_status="Y"');
if(mysqli_num_rows($req)>0)
{
echo "login OK";
}
else
{
echo "The username or password is incorrect";
}

关于php - 如何将我插入的密码与 php 中的 md5 哈希进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33579507/

相关文章:

php - 避免拉伸(stretch)照片

mysql - (mysql) 从 digikam 4.* 迁移到 5.* 时数据库迁移困难

c# - 是否可以复制 .NET HashAlgorithm(用于重复的增量哈希结果)?

vb.net - 如何在 vb.net 中加速 MD5 校验和的生成?

php - 如何在 PHP 中查找数组中的字符串?

PHP MYSQL 防止用户或电子邮件插入两次

php - 关于php的关系数据库问题

mysql - Opencart 基于重量的运输

MySQL 查询在列值上具有不同的值

php - 可以破解 sha1(md5 ('password' )) 密码吗?