password_verify返回false

时间:2017-10-06 19:22:47

标签: php mysql

$query = "SELECT * FROM users WHERE username = '$username'";
    $query_run = mysqli_query($con, $query);

    if(mysqli_num_rows($query_run) < 1)
    {
        header("Location: ../index.php?login=username");
        exit(); 
    }
    else
    {
        if($row = mysqli_fetch_assoc($query_run))
        {
            //deshashing password
            $hashedPwdCheck = password_verify($password, $row['password']);
            if($hashedPwdCheck == false)
            {
                header("Location: ../index.php?login=false");
                exit(); 
            }
            elseif($hashedPwdCheck == true)
            {
                //login the user here
                $_SESSION ['u_name'] = $row['username'];
                $_SESSION ['u_email'] = $row['email'];
                $_SESSION ['u_password'] = $row['password'];
                $_SESSION ['u_id'] = $row['userid'];

                header("Location: ../index.php?login=success");
                exit(); 

以上是登录代码。

以下是存储密码并对其进行哈希处理的注册码。

                    else
                    {
                        //hashing password
                        $hashedPwd = password_hash($password, PASSWORD_DEFAULT);
                        //insert the user into the database
                        $query = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$hashedPwd');";
                        mysqli_query($con, $query);

                        header("Location: ../register.php?register=success");
                        exit();
                    }

请帮助,我有点困惑为什么这不起作用。

此致 罗斯

1 个答案:

答案 0 :(得分:5)

您在其中存储散列密码的列​​不够长。它应该超过60个字符,我建议使用VARCH(255)TEXT来考虑将来可能会创建更长哈希的password_hash()函数的任何更改。

警告

您不应将密码存储在会话数组中,因为它暴露了获取密码被劫持的可能性。

Little Bobby your script is at risk for SQL Injection Attacks. 了解preparedMySQLi语句。

  

&#34;没有sql_injection,因为我的变量上有一个函数可以将输入的文本转换为纯文本。 mysqli_real_escape_string($con, $_POST['registerEmail']);&#34;

即使escaping the string也不安全!

相关问题