i need some help with my code, when i attempt to login it will not provide any error or any problem it will just not send me to the page it’s supposed to after a successful login. This is the code:
<?php
session_start();
include 'conn.php';
// User Authentication
if (isset($_POST['submit'])) {
$username = $_POST['user'];
$password = $_POST['pass'];
// Retrieve user information from the database
$stmt = $conn->prepare("SELECT id_user, password, pin FROM users WHERE username = ?");
$stmt->bind_param("s", $inputUsername);
if ($stmt->execute()) {
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($userId, $hashedPassword, $hashedPin);
$stmt->fetch();
// Verify the entered password
if (password_verify($inputPassword, $hashedPassword)) {
// Password is correct, proceed with authentication
// Check if the user has a PIN
if (!empty($hashedPin)) {
// User has a PIN, ask for PIN
$_SESSION['user_id'] = $userId;
$stmt->close(); // Close the statement before redirecting
header("Location: enter_pin.php");
exit();
} else {
// User doesn't have a PIN, direct to menu
$_SESSION['user_id'] = $userId;
$stmt->close(); // Close the statement before redirecting
header("Location: menu.php");
exit();
}
} else {
echo "<div class='alert alert-danger'>Password is incorrect</div>";
}
} else {
echo "<div class='alert alert-danger'>Username Not Found</div>";
exit();
}
} else {
// Error in executing the statement
die('Error: ' . $stmt->error);
}
}
// HTML Form for User Login
?>
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
<link rel="stylesheet" type="text/css" href="login-style.css">
</head>
<body>
<h1>Login</h1>
<?php
// Display error message if present in the URL
if (isset($_GET['error'])) {
echo "<p>{$_GET['error']}</p>";
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required><br><br>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
I have attempted to change my database and even the login code to others i have found online to see if the code is the problem nut the same issue persists. I’m using xampp and phpmyadmin for my test. ill provide a screenshot of my db for referance and the table. Sorry for my bad english it’s not my main language. db table db
2
Answers
It appears it was a problem in my menu.php, my login form sends the lodein user to the menu but the menu was put in a wrong code. The previous code was:
the code used now is:
There are a few problems here.
You are using
$username
and$password
to get the user inputs, but then you are binding$inputUsername
and$inputPassword
which are not defined.Secondly, your HTML form’s input names are
username
andpassword
, but in your PHP you are using$_POST['user']
and$_POST['pass']
.Final note: Since you are using
isset($_POST['submit'])
, you need to set the name of your submit button:name="submit"
, otherwiseisset($_POST['submit'])
will never be true.