What is the potential issue with storing user data in a PHP session and displaying it to other users?

Storing user data in a PHP session and displaying it to other users can lead to a serious security risk, as sensitive information may be exposed to unauthorized users. To solve this issue, it is important to validate user permissions before displaying any stored data to ensure that only the appropriate user can access their own information.

session_start();

// Check if user is logged in and has permission to view the data
if(isset($_SESSION['user_id']) && $_SESSION['user_id'] == $userId) {
    // Display the user data
    echo "Username: " . $_SESSION['username'];
    echo "Email: " . $_SESSION['email'];
} else {
    echo "You do not have permission to view this data.";
}