Php 不断获得;“密码错误”;登录时
我试图创建一个登录表单,该表单访问MySQL数据库中的详细信息,然后将用户重定向到另一个页面。但是,每当我尝试使用正确的凭据登录时,我总是收到一个错误的密码错误。有人能看到以下代码有什么错误吗Php 不断获得;“密码错误”;登录时,php,mysql,Php,Mysql,我试图创建一个登录表单,该表单访问MySQL数据库中的详细信息,然后将用户重定向到另一个页面。但是,每当我尝试使用正确的凭据登录时,我总是收到一个错误的密码错误。有人能看到以下代码有什么错误吗 <?php // Initialize the session session_start(); // Check if the user is already logged in, if yes then redirect to welcome page if(isset($_SESSION[
<?php
// Initialize the session
session_start();
// Check if the user is already logged in, if yes then redirect to welcome page
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
header("location: index.php");
exit;
}
// Include config file
include "connection.php";
// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
//find first name of logged in user
// Check if username is empty
if(empty(trim($_POST["username"]))){
$username_err = "Please enter username.";
} else{
$email = trim($_POST["username"]);
// Check if the password is empty
if(empty(trim($_POST["password"]))){
$password_err = "Please enter your password.";
} else{
$password = trim($_POST["password"]);
}
// Validate credentials
if(empty($username_err) && empty($password_err)){
// Prepare a select statement
$sql = "SELECT Email, Password FROM users WHERE Email = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_email);
// Set parameters
$param_email = $email;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Store result
mysqli_stmt_store_result($stmt);
// Check if username exists, if yes then verify password
if(mysqli_stmt_num_rows($stmt) == 1){
// Bind result variables
mysqli_stmt_bind_result($stmt, $email, $hashed_password);
if(mysqli_stmt_fetch($stmt)){
if(password_verify($password, $hashed_password)){
// if the password is correct, begin a new session
session_start();
// allocate values to session variables
$_SESSION["loggedin"] = true;
$_SESSION["username"] = $email;
// Redirect user to welcome page
header("location: index.php");
} else{
// Display error message for incorrect password
$message = "Incorrect password, please try again";
echo "<script>
alert('$message');
window.location.href='login.php';
</script>";
exit;
}
}
} else{
// Display an error message if username doesn't exist
$message = "Incorrect username, please try again";
echo "<script>
alert('$message');
window.location.href='login.php';
</script>";
exit;
}
} else{
echo "Something went wrong. Please try again later.";
}
} else {
//prevent SQL Injection
die("Error : " . mysqli_error($conn));
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
}
?>
这可能是我瞎了眼(或者这只是一个片段),但您实际上并没有打开到MySQL的连接,因此$link在您尝试准备语句时将为空。(很抱歉,我没有将此作为评论发布,我对此太陌生了)这可能是我瞎了眼(或者这只是一个片段),但您实际上并没有打开与MySQL的连接,因此$link在您尝试准备语句时将为空。(很抱歉,我没有将此作为评论发布,我对此太陌生了)不相关,但您有两个会话\u start()。不确定这会导致什么session\u start
仅在您还没有会话的情况下启动新会话。您可能打算在登录时使用session_regenerate_id来更改会话id。您的Password
列的DB定义是什么?我将首先查看Password\u verify()
参数是什么,然后单独检查这些参数来调试此问题。旁注:根据电子邮件或密码的正确性给出不同的反馈是个坏主意。这样你就可以把信息泄露给可能的黑客。另外,有些东西是电子邮件地址或用户名,您似乎把这两个混淆了。@KIKOSoftware当我调试它时,它会显示正确的哈希密码和用户输入的正确密码,但您有两个session_start()。不确定这会导致什么session\u start
仅在您还没有会话的情况下启动新会话。您可能打算在登录时使用session_regenerate_id来更改会话id。您的Password
列的DB定义是什么?我将首先查看Password\u verify()
参数是什么,然后单独检查这些参数来调试此问题。旁注:根据电子邮件或密码的正确性给出不同的反馈是个坏主意。这样你就可以把信息泄露给可能的黑客。另外,有些东西可能是电子邮件地址或用户名,你似乎把这两者混在了一起。@KIKOSoftware当我调试它时,它会显示正确的散列密码和用户输入的正确密码这很可能发生在include“connection.php”中
。我确实对此感到奇怪,但如果不能保证实际使用连接,那么尝试打开连接似乎毫无意义,因此决定connection.php可能只包含一个连接字符串或类似内容。这很可能发生在include“connection.php”中
。我确实对此感到奇怪,但如果不能保证实际使用连接,那么尝试打开连接似乎毫无意义,因此decisionconnection.php可能只包含连接字符串或类似内容。