我最近使用我的登录格式,使用下面的$password = md5(password_1)
完整代码将密码存储在数据库中...
然后将其更改为$password = (password_hash($password_1, PASSWORD_DEFAULT);
以下完整代码....
它在我的数据库中成功保存了一个哈希密码,如:$2y$10$.FTJmF/47NbmQMU3nZGTZeKAHYZ8TBm8X2Jc.TLbAIK...
现在验证密码是出错的地方,
原始代码是$password = md5($password_1)
,它将使用存储在第一个代码中的md5成功登录。
我将其更改为$password = password_verify($password_1, PASSWORD_DEFUALT);
但那给了我这个错误。
我试过$hash = password_verify($password_1, PASSWORD_DEFAULT );
但它给了我同样的错误,
<?php
在session_start();
// initializing variables
$username = "";
$email = "";
$errors = array();
// connect to the database
$db = mysqli_connect('', '', '', '');
// 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 loginsystem 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) {
//Hashing the password
$password = password_hash($password_1, PASSWORD_DEFAULT);//encrypt the password before saving in the database
$query = "INSERT INTO loginsystem (username, email, password)
VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: home.php');
}
}
// ...
// ...
// LOGIN USER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password_1']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) $password = password_verify($password, PASSWORD_DEFAULT); {
$query = "SELECT * FROM loginsystem 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: index.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
&GT;
答案 0 :(得分:0)
第一:password_verify()有不同的参数:输入的密码和数据库中的哈希密码。
引导我们进入第二个问题:
您只能在从数据库获取哈希后验证密码。
因此,首先获取该哈希(通过仅查询usename),而不是验证。
$query = "SELECT username, email, password FROM loginsystem WHERE username='$username'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$row = mysqli_fetch_assoc($results);
if(password_verify($password, $row['password'])) {
// password correct!
} else {
// nope
}
} else {
// wrong credentials
}
您还应该更改为prepared statements,因为现在可能容易受到SQL注入攻击。
答案 1 :(得分:-1)
PASSWORD_DEFUALT
拼写错误。它应该是PASSWORD_DEFAULT
,如下例所示:
$hash = password_hash($password, PASSWORD_DEFAULT);
[...]
if (password_verify($password, $hash)) {
// Password matches
}
password_verify
的第一个参数是用户输入的密码。第二个参数是password_hash
生成的存储哈希值。