How can PHP developers ensure that only the authenticated user can edit their own profile information?

To ensure that only the authenticated user can edit their own profile information, PHP developers can implement a check to compare the user ID of the authenticated user with the user ID associated with the profile being edited. If the IDs match, allow the user to edit the profile; otherwise, deny access.

// Check if the user is authenticated
if(isset($_SESSION['user_id'])) {
    // Get the user ID of the authenticated user
    $authenticated_user_id = $_SESSION['user_id'];
    
    // Get the user ID of the profile being edited
    $profile_user_id = // Retrieve the user ID from the database based on the profile being edited
    
    // Compare the user IDs
    if($authenticated_user_id == $profile_user_id) {
        // Allow the user to edit their profile information
    } else {
        // Deny access
        echo "Unauthorized access";
        exit;
    }
} else {
    // Redirect to the login page
    header("Location: login.php");
    exit;
}