【问题标题】:Change Password script in PHP and MD5 hashing [duplicate]在 PHP 和 MD5 散列中更改密码脚本 [重复]
【发布时间】:2011-12-02 22:43:35
【问题描述】:

可能重复:
forget password page, creating a generated password to email to the user.

我正在尝试创建更改密码页面。下面的代码应检查以确保用户在数据库中,确认当前密码并允许他们更改密码。它会正确验证他们的新密码,以确保它们相同且具有一定的长度,但如果用户名不在数据库中,它不会返回错误消息,并且它表示当前密码是在数据库错误。我认为它没有正确匹配哈希密码,但我不确定。谁能帮我解决这些问题。谢谢。

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Change Password Confrim</title>
    </head>
    <body>
        <?php
      $db_server = "server";
       $db_username = "name";
       $db_password = "pass";

       $con = mysql_connect($db_server, $db_username, $db_password);if (!$con)
                {
                    die('Could not connect: ' . mysql_error());
                }

               $database = "User";  

              $er = mysql_select_db($db_username);
        if (!$er) 
        {
         print ("Error - Could not select the database");
         exit;
        }        



//////////Collect the form data ////////////////////
$username =$_P0ST['username'];
$cur_password=$_POST['cur_password'];
$password=$_POST['password'];
$password2=$_POST['password2'];
/////////////////////////



$password = mysql_real_escape_string($password);
$password2 = mysql_real_escape_string($password2);
$username = mysql_real_escape_string($username); 
$cur_password = mysql_real_escape_string($cur_password); 
//Setting flags for checking
$status = "OK";
$msg="";

//Checking to see if password is at least 3 char max 8 
if ( strlen($password) < 3 or strlen($password) > 15 )
{
    $msg=$msg."Password must be more than 3 char legth and maximum 15 char lenght<br/>";
    $status= "NOTOK";
}                   

//Checking to see if both passwords match 
if ( $password <> $password2 )
{
    $msg=$msg."Both passwords are not matching<br/>";
    $status= "NOTOK";
}                   

     $CorrectUser = mysql_query("SELECT * FROM User WHERE username ='$username' AND password = MD5('$cur_password')");
     $row = mysql_fetch_array($CorrectUser);
      if ($row['username'] == $username) 
          {
          $status = "OK"; 
          }
       else {
                print("Your username is not in the database. Please check that you enter the correct username and try again."); 
                $status = "NOTOK";
            }

        if ($row['cur_password'] == MD5('$cur_password')) 
            {
                $status = "OK"; 
            }
         else
             {
                print("You entered the wrong current passowrd"); 
                $status = "NOTOK"; 
             }


if($status<>"OK"){ 
echo "<font face='Verdana' size='2' color=red>$msg</font><br><center><input type='button' value='Retry' onClick='history.go(-1)'></center>";
}
else
{ // if all validations are passed.
    if(mysql_query("UPDATE User SET password = MD5('$password') WHERE username ='$username'"))
    {
        echo "<font face='Verdana' size='2' ><center>Thanks <br> Your password changed successfully.</font></center>";
    }
    else
    {
        echo "<font face='Verdana' size='2' color=red><center>Sorry <br> Failed to change password.</font></center>";
    }

}

?>
<center>
<br><br><a href='Settings.html'>Settings Page</a></center> 

</body>

</html>

【问题讨论】:

  • 尝试比较转义和非转义哈希,因为这可能是问题所在。
  • 请不要转发问题,尤其是当您收到关于第一个问题的有用反馈时
  • 以下行:mysql_select_db($db_username); 应该会导致问题,以防数据库名称与用户名不同
  • 哈希密码时请使用 SHA256 和 salts。另请参阅You're Probably Storing Passwords Incorrectly

标签: php mysql md5


【解决方案1】:

只需从您的 SQL 查询中删除 AND password = MD5('$cur_password')。这样,无论密码是否正确,您都会找到用户。 $row['cur_password'] == MD5('$cur_password') 的 PHP 内测试就足够了;你不需要把它放在你的 SQL 查询中。

【讨论】:

    【解决方案2】:

    PHP 行:

    if ($row['cur_password'] == MD5('$cur_password')) 
    

    应该是

    if ($row['cur_password'] == MD5($cur_password))
    

    但是看起来您已经检查了数据库中的密码。

    此外,如果密码和密码 2 不匹配,则设置 $status = 'NOTOK',但忽略该结果。即:如果您的密码和密码2不匹配,但用户/密码匹配,它仍然会更新。

    除非您需要使用无盐 MD5,否则添加盐可能是明智之举。

    【讨论】:

      【解决方案3】:

      首先,这条线不行

      $CorrectUser = mysql_query("SELECT * FROM User WHERE username ='$username' AND password = MD5('$cur_password')");
      

      这样看……:

      SELECT * FROM User WHERE username ='BOB' AND password = 32RE5446DDGDDDYHBD" <-- THE QUOTES ARE MISSING HERE
      

      试试这样:

      $CorrectUser = mysql_query("SELECT * FROM User WHERE username ='".$username."' AND password = '".MD5($cur_password)."'");
      

      “隔离”变量是一种很好的做法,它可以避免很多错误。

      【讨论】:

      • MySQL(以及几乎每个数据库)都有一个 MD5 函数,所以从技术上讲,查询仍然有效。其次,您的第二个代码块没有任何意义。您不需要引用调用数据库的 MD5 函数的结果。
      猜你喜欢
      • 2014-09-09
      • 2014-11-18
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 2020-08-16
      • 1970-01-01
      相关资源
      最近更新 更多