登录时,即使用户名和密码正确,也会出现错误消息

时间:2016-04-14 01:09:52

标签: php mysqli login passwords

每当我尝试登录时,只要我的用户名和密码正确,该消息总是显示username or password is invalid

这是我的登录php代码。另外,我在注册时使用了password_hash。

<?php
    session_start();
    include_once 'dbcontroller.php';
    if (isset($_POST['submit'])) {
        if (empty($_POST['username']) || empty($_POST['password'])) {
        echo "<script>alert('Please input your username and password'); window.location='index.php';</script>";
        }
        else
        {
            // Define $username and $password
            $username=$conn->real_escape_string(trim($_POST['username']));
            $password=$conn->real_escape_string(trim($_POST['password']));

            $query = $conn->query("select * from users where username='$username'");
            $row = $query->fetch_array();
            if (password_verify($password, $row['password']))
            {
                $_SESSION['login_user']=$row['username'];
                header("location: user/home.php?username=$user_check&request=login&status=success"); // Redirecting To Other Page
            }   
            else{
                echo "<script>alert('The username or password you entered is invalid.'); window.location='index.php';</script>";;
            }
            $conn->close(); // Closing Connection

        }

    }
?>

1 个答案:

答案 0 :(得分:3)

前言:我不是为回复点发布此答案,所以请不要做任何假设。

OP可以使用他自己的判断,如果他想将其标记为已解决,因为该问题尚未删除,并且没有其他人为此发布答案。

  

&#34;我的数据库中只有30个,我会改变它,也许它会起作用。 @JayBlanchard - eric&#34;

这是因为列的长度仅为其应有的50%; 60最小和 MySQL无声地失败

根据手册http://php.net/manual/en/function.password-hash.php

  

&#34;请注意,此常量旨在随着时间的推移而改变,因为新的和更强大的算法被添加到PHP中。因此,使用此标识符的结果长度可能会随时间而变化。因此,建议将结果存储在数据库列中,该列可以扩展到超过60个字符(255个字符将是一个不错的选择)。&#34;

需要做什么。

您需要删除旧的密码/哈希值,更改列的长度并重新开始使用新的哈希值。

现在,根据Jay在评论中包含的链接:

...作为我上周发现的一个链接(并发给他)关于我问自己的一个问题:
&#34;我是否应该转出密码输入?&#34;

嗯,我在网上搜索后找到的链接,回答了我的问题,并提到了其他一些事情。