After clicking the login button It still stay on login.php or redirecting to same file.
I’m new to php language so I couldn’t figure out what’s wrong.
I want to redirect to navbar.php after clicking the login button
can you help me with this? thankyou
here’s the code i have
login.php
<?php
session_start();
// Check if the user is already logged in, if yes then redirect him to welcome page
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
header("location: navbar.php");
exit;
}
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = $login_err = "";
// Processing form data when form is submitted
if (isset($_POST['user_login_btn'])) {
if(isset($_POST['login_btn'])) {
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Process the user login form
$username = mysqli_real_escape_string($con, $_POST['username']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$password = md5($password); // Hash the password before storing it in the database
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($con, $query);
if (mysqli_num_rows($results) == 1) {
// If the login is successful, start a new session and redirect to the user dashboard
session_start();
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location: navbar.php');
} else {
// If the login is unsuccessful, display an error message
array_push($errors, "Wrong username/password combination");
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<div class="header">
<h1>Login</h1>
</div>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<table>
<tr>
<td>Username:</td>
<td><input type="text" name="username" required></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" required></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="login_btn" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>