I have implemented a login system for my website, but I need assistance with a specific feature. I would like to display the username and the associated ranks of the logged-in user on the sidebar. Although my current setup can handle user authentication, I am unsure how to access and show this information from the database.
Here is my signup handler:
<?php
session_start();
include '../database_connection.php';
$firstName = $_POST['first_name'];
$lastName = $_POST['last_name'];
$userId = $_POST['user_id'];
$password = $_POST['password'];
// Validate input fields
if (empty($firstName) || empty($lastName) || empty($userId) || empty($password)) {
header("Location: ../signup.php?error=empty");
exit();
}
// Check if username is already taken
$checkQuery = "SELECT user_id FROM users WHERE user_id='$userId'";
$result = mysqli_query($connection, $checkQuery);
$userExists = mysqli_num_rows($result);
if ($userExists > 0) {
header("Location: ../signup.php?error=username_taken");
exit();
} else {
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$insertQuery = "INSERT INTO users (first_name, last_name, user_id, password)
VALUES ('$firstName', '$lastName', '$userId', '$hashedPassword')";
mysqli_query($connection, $insertQuery);
header("Location: ../home.php");
}
?>
Your session management is broken - that’s why the sidebar won’t show up. You’re storing $userRecord['id'] in the login script but trying to access username and rank data that was never saved to the session. I hit this same issue last year when building something similar. Fix your login script by adding these session variables right after password verification: $_SESSION['username'] = $userRecord['user_id']; and $_SESSION['rank'] = $userRecord['rank'];. For the sidebar, just create a PHP include that checks if these session variables exist before displaying them. Skip arrays for rank conversion - use a switch statement instead. It’s way easier to maintain and handles defaults better: switch($_SESSION['rank']) { case 1: echo 'Administrator'; break; case 2: echo 'Moderator'; break; default: echo 'Member'; }. Works great in production.
your login script is good, but u gotta add the user data to the session. after verifying the pw, include $_SESSION['username'] = $userRecord['user_id']; and $_SESSION['rank'] = $userRecord['rank'];. to convert ranks, use an array: $ranks = [1=>'admin', 2=>'user']; then just echo it: echo $ranks[$_SESSION['rank']];.
First, store all your needed data in the session. In sidebar.php, check if session vars exist, then query the db for fresh data: $sql = "SELECT user_id, rank FROM users WHERE id = '{$_SESSION['user_id']}'"; Skip the arrays and just use simple if statements for ranks - if($rank == 1) echo 'admin'; elseif($rank == 2) echo 'moderator'; and so on.
To properly manage user sessions, ensure that you are storing the username and rank upon successful login. You can add the following lines: $_SESSION['user_id'] = $userRecord['id'];, $_SESSION['username'] = $userRecord['user_id'];, and $_SESSION['rank'] = $userRecord['rank'];. To convert numerical ranks to titles, consider implementing a function like this: function getRankTitle($rank) { $titles = [1 => 'Admin', 2 => 'User', 3 => 'Subscriber', 4 => 'Member', 5 => 'Guest']; return $titles[$rank] ?? 'Unknown'; }. This method will allow you to display the correct rank in your sidebar.