I wanna ask what is wrong with my code. When the user is not logged in, the user icon on index.php nav bar will redirect to customer.php(login). And after they login, it will go back to index.php and the user icon will contain the dropdown.
The problem is that after I try to login, when I click the user icon, my homepage just reloads. Here is my code:
header.php
<li <?php if($current_page == "login") echo "class='login'" ?> class="nav-item dropdown">
<?php
if(isset($_SESSION['user_id'])) { ?>
<a href="#" class="nav-link dropdown-toggle" id="userDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<i class="bi bi-person-circle text-white"></i>
</a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="userDropdown">
<li><a class="dropdown-item" href="customer_profile.php"><i class="bi bi-person-badge-fill me-2"></i>My Profile</a></li>
<li><a class="dropdown-item" href="customer_orders.php"><i class="bi bi-box-fill me-2"></i>My Orders</a></li>
<li><a class="dropdown-item" href="customer_settings.php"><i class="bi bi-gear-fill me-2"></i>Settings</a></li>
<li><a class="dropdown-item" href="notifications.php"><i class="bi bi-bell-fill me-2"></i>Notifications</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item" href="customer_logout.php"><i class="bi bi-box-arrow-left me-2"></i>Logout</a></li>
</ul>
<?php } else { ?>
<a href="customer.php" class="nav-link">
<i class="bi bi-person-circle text-white"></i>
</a>
<?php } ?>
</li>
customer.php
<?php
$current_page = "login";
require 'user-config.php';
if(isset($_SESSION["user_id"])) {
header("location: index.php");
exit();
}
if(isset($_POST["submit"])) {
$usernameemail = mysqli_real_escape_string($conn, $_POST["usernameemail"]);
$password = mysqli_real_escape_string($conn, $_POST["password"]);
$result = mysqli_query($conn, "SELECT * FROM users WHERE username = '$usernameemail' OR email = '$usernameemail'");
if(mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
if(password_verify($password, $row["password"])) {
$_SESSION["user_id"] = $row["user_id"];
header("location: index.php");
exit();
} else {
echo "<script> alert('Wrong password.'); </script>";
}
} else {
echo "<script> alert('User is not registered.'); </script>";
}
}
?>
<!--Other content-->
<!--Navigation Bar-->
<?php require_once 'customer/includes/header.php'; ?>
<!--End of Navigation Bar-->
<!-- end snippet -->