【问题标题】:Encrypted Password Stores in Database But Apparently Wrong?数据库中的加密密码存储但显然错误?
【发布时间】:2019-07-19 02:45:10
【问题描述】:

我使用教程创建了一个简单的登录,在教程中使用 md5 加密,而不是 md5 我使用 password_hash,

$password = password_hash("$password_1", PASSWORD_DEFAULT);

它可以工作并将加密密码存储在我的数据库中,但是当我尝试使用非加密密码登录时,我得到“输入用户名或密码”,我使用的代码如下 - 只有我删除了我的服务器信息:

<?php
session_start();

// initializing variables
$username = "";
$email    = "";
$errors = array(); 

// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);

// form validation: ensure that the form is correctly filled ...
// by adding (array_push()) corresponding error unto $errors array
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}

// first check the database to make sure 
// a user does not already exist with the same username and/or email
$user_check_query = "SELECT * FROM users WHERE username='$username' OR 
email='$email' LIMIT 1";
$result = mysqli_query($db, $user_check_query);
$user = mysqli_fetch_assoc($result);

if ($user) { // if user exists
if ($user['username'] === $username) {
  array_push($errors, "Username already exists");
}

if ($user['email'] === $email) {
  array_push($errors, "email already exists");
}
}

// Finally, register user if there are no errors in the form
if (count($errors) == 0) {
$password = password_hash("$password_1", PASSWORD_DEFAULT);//encrypt the 
password before saving in the database
$query = "INSERT INTO users (username, email, password) 
          VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: testlogin.php');
 }
}

// ... 

// LOGIN USER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);

if (empty($username)) {
    array_push($errors, "Username is required");
}
if (empty($password)) {
    array_push($errors, "Password is required");
}

if (count($errors) == 0) {
    $password = password_hash("$password", PASSWORD_DEFAULT);
    $query = "SELECT * FROM users WHERE username='$username' AND 
 password='$password'";
    $results = mysqli_query($db, $query);
    if (mysqli_num_rows($results) == 1) {
      $_SESSION['username'] = $username;
      $_SESSION['success'] = "You are now logged in";
      header('location: testlogin.php');
    }else {
        array_push($errors, "Wrong username/password combination");
    }
  }
 }

?>

为了让 password_hash 正常工作,我需要包含什么内容吗?

【问题讨论】:

  • 关键基本的东西:password_hash在你第一次保存用户时使用。然后你在登录时使用password_verify
  • 所以在您的登录块中,首先选择用户名或电子邮件是否存在,如果存在,则获取该行,输入密码列并使用password_verify(&lt;input password&gt;, &lt;hashed password from db&gt;) 检查用户输入的密码是否正确

标签: php authentication encryption


【解决方案1】:

验证用户密码时,需要使用password_verify函数。我在下面修改了您的登录代码:

if (count($errors) == 0) {
    $query = "SELECT * FROM users WHERE username='$username'";
    $result = mysqli_query($db, $query);

    if (mysqli_num_rows($result) === 1) {
        $user = mysqli_fetch_assoc($result);

        if (password_verify($password, $user['password'])) {
            $_SESSION['username'] = $username;
            $_SESSION['success'] = "You are now logged in";
            header('Location: testlogin.php');
            die();
        }
    }

    array_push($errors, "Wrong username/password combination");
}

【讨论】:

  • 你是个传奇人物,非常感谢您将其放入代码中!完美运行。
  • 由于某种原因我不得不等待 2 分钟才能接受
猜你喜欢
  • 2012-04-07
  • 1970-01-01
  • 2020-03-20
  • 2011-04-26
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
  • 2016-07-15
  • 2011-04-28
相关资源
最近更新 更多