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

时间:2015-11-07 05:34:39

标签: 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;
}





?>

4 个答案:

答案 0 :(得分:1)

&#13;
&#13;
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";
}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您必须检查数据库密码长度,MD5

必须为32长度

答案 2 :(得分:1)

首先,您必须检查数据库类型和长度,然后将密码类型和长度/值设置为32。

<a href="#">

答案 3 :(得分:1)

md5不应该用于密码,它已被彩虹表示死亡。如果您使用的是PHP 5.5或更高版本,则内置函数可用于生成和验证密码哈希值。如果您使用的是早于5.5的PHP版本,则可以使用向后兼容的库。

旧版PHP的向后兼容库(我希望没有人使用5.3之前的任何版本的PHP)和PHP 5.5及更新版本(http://php.net/manual/en/ref.password.php)的内置函数具有函数用于验证提交的密码。

用户提交的数据一旦验证最好与预准备语句一起使用,因为它可以消除SQL注入攻击的风险。

运行任何查询时,需要进行错误处理(现代方法是使用异常),如果查询失败,请处理任何错误。在某些情况下,查询失败可能是需要回滚事务,在某些情况下,您可能需要终止脚本的执行

请勿忘记确保users表中的密码字段长度足以处理整个哈希值,否则会被截断并且所有用户都将被拒绝访问