I am new to PHP and have been having a few problems. I am not necessarily facing any errors, but I cannot seem to figure out how to change the nav bar once the user is logged in. At this moment, I've had to essentially re-create the 'index.php' and name it 'home.php' which users are redirected to upon successful login. Well, they're actually redirected to 'loginredirect.php' first, where they click a 'next' button to be redirected to 'home.php'. I do not actually want the 'home.php' as that would mean I would have to basically create two entire websites and the user would always be logged out when redirected back to the index.php. I have included the code below for 'home.php', 'loginredirect.php' and 'functions.php' (which is where I store all PHP functions) (I will only post the relevant code).
(Please don't mind where it says 'Jimi Hendrix'😂 I just haven't figured out what I'll call that page yet)
What I have tried:
Functions.php:
function loginUser($username, $password){
$mysqli = connect();
$username = trim($username);
$password = trim($password);
if($username == "" || $password == ""){
return "Both fields are required";
}
$username = filter_var($username, FILTER_SANITIZE_STRING);
$password = filter_var($password, FILTER_SANITIZE_STRING);
$sql = "SELECT username, password FROM users WHERE username = ?";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
if($data == NULL){
return "Wrong username";
}
if(password_verify($password, $data['password']) == FALSE) {
return "Wrong password";
}else{
$_SESSION['user'] = $username;
header("location: loginredirect.php");
exit();
}
}
home.php (nav bar):
<header>
<a href="#" class="logo"><img src="images/logo4.png" alt=""></a>
<ul class="navmenu">
<li><a href="#" class="hover-underline-animation">Home</a></li>
<div class="dropdown1">
<button class="dropbtn1">
<li class="dropdown dropdown-4"><a href="#" class="hover-underline-animation">Shop</a></li>
</button>
<div class="dropdown-content1">
<a href="#">For Android</a>
<a href="#">For iOS</a>
<a href="#">For PC</a>
<a href="#">For Television</a>
<a href="#">Cables</a>
<a href="#">Smartphone Cases</a>
<a href="#">Audio</a>
</div>
</div>
<li><a href="#" class="hover-underline-animation">Sale & Offers</a></li>
<li><a href="#" class="hover-underline-animation">Contact Us</a></li>
<li><a href="#" class="hover-underline-animation">Our Policies</a></li>
<div class="dropdown">
<button class="dropbtn">
<li><a href="#" class="hover-underline-animation">More</a></li>
</button>
<div class="dropdown-content">
<a href="#">About Us</a>
<a href="#">FAQs</a>
<a href="#">Jimmy Hendrix</a>
</div>
</div>
</ul>
<div class="nav-icon">
<a href="#">class="bx bx-search-alt-2">
<div class="dropdown2">
<button class="dropbtn2">
<a href="#">^__i class='bx bx-user'></a>
</button>
<div class="dropdown-content2">
<div class="login">
<ul>
<a href="">User Profile</a>
<a href="">Log out</a>
</ul>
</div>
</div>
</div>
<a href="#">^__i class='bx bx-shopping-bag'></a>
<div class="bx bx-menu" id="menu-icon"></div>
</div>
</header>
home.php (php code):
<?php
session_start();
require "../db_conn.php";
require "../functions.php";
?>
loginredirect.php:
<?php
session_start();
require "db_conn.php";
require "functions.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>iMobi Tech | User Registered</title>
<link rel="stylesheet" href="redirect_style.css">
</head>
<body>
<div class="container">
<div class="content">
<div class="popup">
<h3>Hello, <span><?php echo $_SESSION['user'] ?></span></h3>
<p>You have logged into your Account.<br>Please click 'next' in order to continue.</p>
<a href="loggedin/home.php" class="btn">Next</a>
</div>
</div>
</div>
</body>
</html>
Before I created the 'home.php' I tried to create the separate nav bar in 'index.php'. Here is the code I tried (will only include specific code):
<?php
session_start();
require "../db_conn.php";
require "../functions.php";
?>
<?php
if (isset($_SESSION["user"]) && ($_SESSION["user"] == FALSE)){
?>
<div class="dropdown-content2">
<div class="login">
<ul>
<a href="login.php">Log in</a>
<a href="user_registration.php">Register</a>
</ul>
</div>
</div>
<?php }else{ ?>
<div class="dropdown-content2">
<div class="login">
<ul>
<a href="#">User Profile</a>
<a href="#">Log out</a>
</ul>
</div>
</div>
<?php } ?>
However, all that did was automatically set the nav bar to the 'User Profile/Log out' rather than the 'Register/Login'. I want the default to display 'Register/Login' and the logged in version to display 'User Profile/Log out'. Can anyone please help me with this? I am new to PHP so I don't quite understand it very well, so if you could please break it down to me and also tell me why it isn't working, not just how to fix it, that would be highly appreciated. Although I am looking for an answer, I would also like to actually learn, so I am not asking for anyone to just 'do my homework', I genuinely want to be able to understand. Thank you so much.