doulu3808 2015-12-03 11:49
浏览 130
已采纳

PHP中的password_verify无法使用数据库

I am trying to make login form work using password_hash and password_verify function but I am not able to verify password due to some syntax error.

The following is my registration script

<?php
    include("includes/connection.php");
        if(isset($_POST['reg'])) {

        //registration form
        $u_email = mysqli_real_escape_string($con, $_POST['email']);
        $u_pass = mysqli_real_escape_string($con, $_POST['password']);


        $get_email = "SELECT * FROM user WHERE user_email='$u_email'";
        $run_email = mysqli_query($con, $get_email);
        $check = mysqli_num_rows($run_email);  



        if($check==0){   


        $hashed_pass = password_hash('u_pass', PASSWORD_BCRYPT, array('cost' => 10));  

        $insert = "insert into user (user_email, user_pass) values ('$u_email', '$hashed_pass')";

        $run_insert = mysqli_query($con, $insert);

        if($run_insert) {
            echo "<script>alert('Registration Successful!')</script>";
        }
        else {
            echo "<script>alert('Registration not Successful!')</script>";

        }
        }
        else{

        echo "Email already registered";
        }

        }


?>

The following is the login script

<?php

    include("includes/connection.php");

        if(isset($_POST['login'])) {

            $u_email = mysqli_real_escape_string($con,$_POST['u_email']);
            $u_pass = mysqli_real_escape_string($con,$_POST['u_pass']);

            $get_user = "select user_pass from user where user_email='$u_email'";
            if(password_verify($u_pass, $user_pass)){

                $_SESSION['user_email']=$u_email;
                echo "<script>alert('Login successfull')</script>";
            }
            else {
                echo "<script>alert('Password or email is not correct!')</script>";
            }

        }
?>

It is just showing ('Password or email is not correct!') even though login details are correct.

  • 写回答

1条回答 默认 最新

  • doushi1900 2015-12-03 12:13
    关注

    You are not passing the user password into password_hash() function. The first parameter should be the password i.e. $_POST['password'] not 'u_pass'.

    $hashed_pass = password_hash($_POST['password'], PASSWORD_BCRYPT, array('cost' => 10));
    

    Also be careful about SQL escaping. You should only escape strings that are going into a SQL string. In your code you cannot use $u_pass in password_hash() because you have used mysqli_real_escape_string so it's no longer the string the user entered.

    Your login script also has a few bugs.

    if(password_verify($u_pass, $user_pass)){
    

    $u_pass has been through mysqli_real_escape_string so is not what the user entered. Also $user_pass will not be defined as you have not run the SQL and assigned user_pass to $user_pass.

    I'd suggest taking a step back and leaving the password hashing out while you develop the code. Get it working with plain passwords stored in the database. Then you can verify it is saving the email/password correctly by looking in the database. Once it is working then add the password hashing code in.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号