index.php:
<?php
// index.php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Retrieve the submitted login form data
$username = $_POST['username'];
$password = $_POST['password'];
// Validate the login credentials
// Query the database to check if the username and password match
// You'll need to replace 'citizens' with the actual table name and adapt the query accordingly
// Assuming you're using MySQLi
$mysqli = new mysqli('localhost', 'root', '', 'online_fir');
$stmt = $mysqli->prepare("SELECT id, password FROM police WHERE Email = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows === 1) {
// User found, verify the password
$stmt->bind_result($id, $storedPassword);
$stmt->fetch();
if ($password === $storedPassword) {
// Store relevant information in the session
$_SESSION['user_id'] = $id;
$_SESSION['username'] = $username;
// Redirect the user to the welcome page or perform other actions
header("Location: home.php");
exit();
}
}
// If the login credentials are invalid, show an error message or redirect back to the login page
$error = "Invalid username or password.";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>FIR</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<header>
<div class="navbar">
<a href="index.php">
<div class="logo">
<img src="img/logo.jpg" alt="Logo" oncontextmenu="return false;">
<span class="logo-text">NYPD</span>
</div>
</a>
</div>
</header>
<div class="login-container">
<h2 class="login-title">Login</h2>
<form method="POST" action="index.php">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" id="username" name="username">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
</div>
<div class="error">
<?php if (isset($error)) { ?>
<p<p style="color:red;"><?php echo $error; ?></p>
<?php } ?>
</div>
<button type="submit">Login</button>
<a href="register.php">Register User</a>
</form>
</div>
</body>
</html>
This is my code. Everything worked fine few days before. Now it’s not working. When I try to login, it says “Invalid username or password” even if the username and the password is correct. Anyone please help.
PS: I’m using XAMPP Server. And I’ve stored the password in the database as plain text.
I tried to login. But its not logging-in. It says “Invalid username or password” even if the username and the password is correct.