php - 获取不正确的密码
问题描述
我不知道为什么我的密码不正确,我的注册页面工作正常。每次我尝试登录时,它都会在我的 url 中显示 login=incorrect password。我试图在网上找出所有可能的问题,但没有任何帮助我。
<?php
session_start();
if(isset($_POST['submit'])){
include_once 'dbt.inc.php';
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
//error handlers
if(empty($username) || empty($password)){
header("Location: ../main_login.php?login=empty");
exit();
}
else{
$sql = "SELECT * FROM users WHERE user_username = '$username'";
$run = mysqli_query($conn, $sql);
$result = mysqli_num_rows($run);
if ($result < 1) {
header("Location: ../main_login.php?login=error");
exit();
}
else{
if ($row = mysqli_fetch_assoc($run)) {
$hashedpasswordcheck = password_verify($password, $row['user_password']);
if ($hashedpasswordcheck == false) {
header("Location: ../main_login.php?login=incorrect password");
exit();
}
elseif($hashedpasswordcheck == true){
//log in user
$_SESSION['user_id'] = $row['user_id'];
$_SESSION['user_first'] = $row['user_first'];
$_SESSION['user_last'] = $row['user_last'];
$_SESSION['user_email'] = $row['user_email'];
$_SESSION['user_username'] = $row['user_username'];
$_SESSION['user_password'] = $row['user_password'];
header("Location: ../main_login.php?login=success");
exit();
}
}
}
}
}
else{
header("Location: ../main_login.php?login=error");
exit();
}
?>
这是我的注册码它工作得很好,我没有在这个代码中看到任何错误,所以如果你看到任何错误,请帮助我
<?php
if(isset ($_POST['submit'])){
include_once 'dbt.inc.php';
$first = mysqli_real_escape_string($conn, $_POST['first']);
$last = mysqli_real_escape_string($conn, $_POST['last']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
//Error Handlers
if(empty($first) || empty($last) || empty($email) || empty($username) || empty($password)){
header("Location: ../main_signup.php?signup=empty");
exit();
}
else{
//check if input characters are valid
if(!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)){
header("Location: ../main_signup.php?signup=invalid first and last name");
exit();
}
else{
//check if email is valid
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
header("Location: ../main_signup.php?signup=invalid_email");
exit();
}
else{
$sql = "SELECT * FROM users WHERE user_username='$username'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0){
header("Location: ../main_signup.php?signup=username already taken");
exit();
}else{
//hashing the password
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
//insert the user into database
$sql = "INSERT INTO users (user_first, user_last, user_email, user_username, user_password) VALUES('$first','$last','$email','$username','$hashed_password ')";
$run = mysqli_query($conn, $sql);
header("Location: ../main_signup.php?signup=success");
exit();
}
}
}
}
}
else{
header("Location: ../main_signup.php");
}
?>
解决方案
您在注册代码中的 hashed_password 变量后面有一个空格。
'$username','$hashed_password ')";
推荐阅读
- git - git status 忽略换行符和空格的变化
- reactjs - framer-motion ios 9 兼容性
- identityserver4 - Identity Server 4:使用 EntityFramework Core 进行配置和操作数据
- laravel - 提交表单时为 foreach() 提供的参数无效
- fb-hydra - fb-hydra:如何实现 2 个嵌套的结构化配置?
- graphql - 带有 graphql 的 Keystone v5
- powershell - Powershell:选择字符串和正则表达式
- c# - 在应用程序资源的文本块中绑定 IDataErrorInfo 错误
- sqlalchemy - SQLAlchemy 自动刷新在 FastAPI 中不起作用
- c++ - 为什么删除有效的堆分配指针会产生“free():无效指针”错误?