【发布时间】: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(<input password>, <hashed password from db>)检查用户输入的密码是否正确
标签: php authentication encryption