Is storing the username in a session variable a good practice for maintaining user login status in PHP?

Storing the username in a session variable is a common practice for maintaining user login status in PHP. This allows the server to remember the user's identity across different pages without the need to repeatedly ask for login credentials. However, it's important to ensure that the session is properly secured to prevent unauthorized access.

// Start the session
session_start();

// Check if the user is logged in
if(isset($_SESSION['username'])) {
    // User is logged in, perform necessary actions
} else {
    // User is not logged in, redirect to login page
    header("Location: login.php");
    exit();
}