duande3134
duande3134
2019-04-06 07:42
浏览 69
已采纳

无法在MySQL中获取登录表单的哈希密码

I made a login form, and while the registration form is fully functional, the login form doesn't seem to be working. $user and $pwd get declared, but $pwd_hashed stayes empty.

if(isset($_GET['login'])) {
   $user = $_POST['user'];
   $pwd = $_POST['pwd'];

   $stmt = $pdo->prepare("SELECT * FROM users WHERE user = :user");
   $result = $stmt->execute(array('user' => $user));
   $pwd_hash = $stmt->fetch();

   if ($pwd_hash !== false && password_verify($pwd, $pwd_hash['password'])) {
      $_SESSION['userid'] = $pwd_hash['id'];
      die('Login successful<br>');
   } else {
      $errorMessage = "Username or password doesn't match<br>";
   }
}
if(isset($errorMessage)) {
   echo $errorMessage;
}

Should the code work like this and I just got something wrong with naming, or is there an error in the code?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpqmu84646
    dpqmu84646 2019-04-06 10:40
    已采纳

    This form is included in another site, in which a database was already selected. By removing it (;dbname=test) and specifying the users data base (users.users) the issue is gone.

    点赞 评论

相关推荐