What are the advantages of using a whitelist approach in PHP to determine which pages should include specific content?

When determining which pages should include specific content in PHP, using a whitelist approach can provide better security and control over what content is displayed. By explicitly listing the pages that are allowed to include the content, you can prevent unauthorized access or injection of malicious scripts.

$allowed_pages = array('page1.php', 'page2.php', 'page3.php');

$current_page = basename($_SERVER['SCRIPT_NAME']);

if (in_array($current_page, $allowed_pages)) {
    // Include specific content here
} else {
    // Redirect or display an error message
    echo "Access denied";
}