douye2020 2017-09-11 16:52
浏览 57
已采纳

php加密密码然后验证不起作用[重复]

This question already has an answer here:

So this is how I encrypt my password.

$password_encrypted = password_hash($password, PASSWORD_DEFAULT);

And this is how I get the value and check it:

<?php
    include("config.php");
    session_start();

    if($_SERVER["REQUEST_METHOD"] == "POST") {
        // username and password sent from form 

        $myusername = mysqli_real_escape_string($db,$_POST['uname']);
        $mypassword = mysqli_real_escape_string($db,$_POST['psw']); 

        $result = mysqli_query(
            $db,
            "SELECT password FROM interna_dostop WHERE up_name = '$myusername'");

        $row = mysqli_fetch_array($result);
        $hash = $row['password'];
        echo $hash;
        echo $mypassword;
        echo $myusername;

        if (password_verify('$mypassword','$hash')) {
            header('Location: another.php'); exit;
        } else {
            echo 'Invalid password.';
        }
    }
?>

Now here is the funny part. This returns false. But if I enter in password_verify ('mypass', 'encrypted_pass') I get true?

Any ideas?

</div>
  • 写回答

2条回答 默认 最新

  • dougui5419 2017-09-11 16:55
    关注

    You're passing $mypassword and $hash in with single quotes to password_verify(). No need for the single quotes.

    password_verify($mypassword,$hash)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类