ich habe ein Login-System in PHP gebaut, aber aus irgendeinem Grund funktioniert die Passwortüberprüfung mit password_verify nicht, obwohl die Eingaben korrekt zu sein scheinen. Hier ist der relevante Codeabschnitt:
<?php
// login.php
session_start();
include 'db.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_SPECIAL_CHARS);
$password = filter_input(INPUT_POST, "password", FILTER_SANITIZE_SPECIAL_CHARS);
$stmt = $conn->prepare("SELECT * FROM user WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
if ($user && password_verify($_POST['password'], $user['password'])) {
$_SESSION['id_user'] = $user['id_user'];
$_SESSION['role'] = $user['role'];
header('Location: index.php');
} else {
echo "Invalid credentials";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form method="POST">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<button type="submit">Login</button>
</form>
</body>
</html>