When should a 403 'Forbidden' header code be used in PHP?

The 403 'Forbidden' header code should be used in PHP when a user is trying to access a resource that they do not have permission to access. This could be due to authentication issues, insufficient privileges, or other security concerns. By sending a 403 header code, you are informing the user that they are not allowed to access the requested resource.

<?php
// Check if user has permission to access the resource
if(!userHasPermission()) {
    header("HTTP/1.1 403 Forbidden");
    exit;
}

// Function to check user permission
function userHasPermission() {
    // Add your permission logic here
    return false; // Return true if user has permission, false otherwise
}
?>