What potential security risks are involved in displaying user details based on user ID?

Displaying user details based on user ID can potentially expose sensitive information if the user ID is not properly validated or authenticated. This can lead to unauthorized access to personal data, such as email addresses, phone numbers, or even financial information. To mitigate this risk, always ensure that the user requesting the details is authenticated and authorized to view the information before displaying any user details.

// Validate user authentication and authorization before displaying user details
if ($authenticated_user_id == $requested_user_id) {
    // Display user details
    echo "User ID: " . $user['id'] . "<br>";
    echo "Username: " . $user['username'] . "<br>";
    echo "Email: " . $user['email'] . "<br>";
} else {
    echo "You are not authorized to view this user's details.";
}