douguo6472 2016-08-10 09:04
浏览 49
已采纳

密码与我的PHP登录表单不匹配

I have tried many times but every time its says Email & Password Not Matched!.I can not find any solution of it.I have used PDO for database connection.I think i have problem with session value.But i can not find the error.I have tried so far:

<?php
/* For Login */

include 'db.php';
if (isset($_POST['login'])) {
    $email = $_POST['email'];
    $hash  = $_POST['password'];
    $password  = password_hash($hash, PASSWORD_DEFAULT);
    $records = $db->prepare("SELECT id FROM users WHERE email=? AND password=?");
    $records->execute(array($email,$password));
    $userdata = $records->fetch(PDO::FETCH_ASSOC);
    if (!empty($email) && !empty($hash)) {
        if (count($userdata)>0 ) {
            //session_start();
            $_SESSION['email'] = $userdata['email'];
            $_SESSION['password'] = $userdata['password'];
            if ($_POST['password'] == $userdata['password']) {
                echo "<div class='alert alert-success text-center'>
                      <strong>Successfully</strong> Login</div>";
                exit;
            }else{
                echo "<div class='alert alert-danger text-center'> 
                      <strong>Email & Password</strong> Not Matched!
                      </div>";
            }
        }else{
            echo "Email and Password Not Matched!";
            echo $_SESSION['email'];
        }
    }else{
        echo "<div class='alert alert-danger text-center'>
              <strong>Email & Password</strong> must not be empty!</div>";
    }
}
?>

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongsha2792 2016-08-10 09:32
    关注

    You are re hashing the incoming password, that is not how its done with password_hash() you need to use password_verify() to test that the hashed password on the database in the same as the plain text password passed in by the user.

    If you run password_hash() using the same password twice in a row you will get a different hash each time, therefore you must use password_verify() to verify the entered password against the hashed one on the database.

    Try this from the CLI and it will demonstrate:

    $password = 'WoopsADaisy';
    echo password_hash($password, PASSWORD_DEFAULT).PHP_EOL;
    echo password_hash($password, PASSWORD_DEFAULT).PHP_EOL;
    

    The output, only when I ran it as a test returns

    $2y$10$t52UeqDlP897iD/n6uo71OErIFP5c4wsli6gLRNdfyQLkv8m6Wxs2
    $2y$10$FczpgQIsmpzr6.vhYA6d3.QmEe3i3HR4WkapLrtO/BLK4Uul0WjUa
    

    You wont get the same hashes, but you will get 2 different hashes!

    This also means you need to mod your process a little

    <?php
    /* For Login */
    
    // always start the session at the top of a script
    session_start();
    
    include 'db.php';
    
    // isset can test many variables at the same time
    if (isset($_POST['login'], $_POST['email'], $_POST['password'])) {
        $email      = $_POST['email'];
        $plain_pwd  = $_POST['password'];
    
        // get hashed password to test
        // also you were never actually retrieving the email
        $records = $db->prepare("SELECT id,password,email 
                                 FROM users WHERE email=?");
        $staus = $records->execute(array($email));
    
        // test the query actually worked
        if ( $staus === false ) {
            print_r($db->errorInfo());
            exit;
        }
    
        $userdata = $records->fetch(PDO::FETCH_ASSOC);
    
    
    
        if ( password_verify($plain_pwd, $userdata['password']) ) {
            // the entered password is GOOD
    
            $_SESSION['email'] = $userdata['email'];
    
            // never need to store in session
            //$_SESSION['password'] = $userdata['password'];
    
            echo "<div class='alert alert-success text-center'>
                  <strong>Successfully</strong> Login</div>";
        } else {
            echo "Email and Password do not Match!";
        }
    
    }else{
        echo "<div class='alert alert-danger text-center'>
              <strong>Email & Password</strong> must not be empty!</div>";
    }
    ?>
    

    Something else that can trip you up when hashing passswords. Make sure that the column password on your table is long enough to hold the hashed password. 60 chars in what password_hash() created using PASSWORD_DEFAULT but as the default hash algorithm may change over time should it need strengthening, it is recommended to make this column a varchar(255) to keep your database future proof.

    References :

    password_hash()

    password_verify()

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

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?