How can the username be retrieved after a session login in PHP?

After a user logs in to a session in PHP, the username can be retrieved by accessing the $_SESSION superglobal array where the username is stored during the login process. To retrieve the username, simply access the 'username' key in the $_SESSION array.

// Start the session
session_start();

// Check if the username is set in the session
if(isset($_SESSION['username'])) {
    $username = $_SESSION['username'];
    echo "Welcome, $username!";
} else {
    echo "Username not found in session.";
}