使用password_verify和password_hash的密码无效

时间:2018-11-13 23:25:31

标签: php mysql forms passwords

我正在写一个登录页面。我的数据库中存储的密码使用password_hash加密。当我尝试比较输入的密码和存储的密码时,它们不匹配。

这是我的表格:

<input type="text" class="form-control" id="inlineFormInput"    placeholder="Jane Doe" name="username" required>

 <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" name="password" required>

...这是我的PHP代码:

<?php 
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "lab2";

        if($dbc=@mysqli_connect($servername,$username,$password,$dbname)){
            print "connected";
            print "<br>";
            $q = "select * from users where username = '". $_POST['username'] ."'
            ";

            if($result=@mysqli_query($dbc,$q)){
            $row = mysqli_fetch_array($result);
                if(password_verify($_POST['password'],$row['password'])){
                    print "you are logged in!";
                    print "<br>";
                }else {
                    print "username or password error";
                    //header('Refresh: 2;login.php');
                }



            }else{
                print "error occured: ";
                print mysqli_error($dbc);
            }


        }else {
            print "connection error: ";
            print mysqli_connect_error();
        }




        ?>

这是我创建哈希密码的方式

$password = $_POST['password'];
            $hashed_password = password_hash($password, PASSWORD_DEFAULT);
               $q = "insert into users (username,email,password,date)
                values ('".$_POST['username']."','".$_POST['email']."','$hashed_password','$date')
                "; 

0 个答案:

没有答案