douwei1921 2016-03-29 13:30
浏览 50

我无法让php bcrypt工作,我不知道为什么

I've been upgrading my script to use bcrypt for authentication and I cannot get it to work. Here's some sample code I have.

<?php
require '../functions/config.php.inc';
if ($_POST[login]) {
    $username = mysql_real_escape_string($_POST['username']);
    $typedpass = mysql_real_escape_string($_POST['password']);
    $info = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
    $data = mysql_fetch_array($info);
    $storedpass = $data['password'];

    //this code block is for stack overflow (it saves the entered password into a bcrypt hash then fetches the database row)
    $darealhash = password_hash($typedpass, PASSWORD_DEFAULT)."
";
    $result5 = mysql_query("UPDATE users SET password='$darealhash' WHERE username='$username'") or die(mysql_error());
    $info = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
    $data = mysql_fetch_array($info);
    $storedpass = $data['password'];
    print_r($data);echo "<hr>";
    echo "darealhash is $darealhash // storedpass is $storedpass // username is $username<br>";

    if(password_verify("$typedpass", $storedpass)){
        echo "correct password";
    } else {
        echo "WRONG password!"; 
    }
}
?>
<form method="POST">
  <table border="0">
  <tr>
  <td>
  Username: <br><input type="text" size="15" class="bigtext" maxlength="25" name="username">
  </td>
  </tr><tr>
  <td>
  Password: <br><input type="password" size="15" class="bigtext"  maxlength="25" name="password">
  </td>
</tr>
 <tr><td align="right">  <input type="submit" name="login" class="bigbutton"  value="Login"></td></tr>

  </table></form>
</body>

</html>

What the code above does is save whatever password is being used in the login form to the database in a bcrypt hash, then the bcrypt hash in the database is compared against the plaintext enter of the password, and if correct it echoes "correct password" but it keeps echoing "wrong password" and I don't know why.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀