How can a Whitelist approach be used to control the inclusion of content on specific pages in PHP?

To control the inclusion of content on specific pages in PHP using a Whitelist approach, you can create an array of allowed page names and check if the current page is in the whitelist before including the content. This helps prevent unauthorized content from being included on certain pages.

// Define an array of allowed page names
$allowed_pages = array('home', 'about', 'contact');

// Get the current page name
$current_page = basename($_SERVER['PHP_SELF']);

// Check if the current page is in the whitelist
if (in_array($current_page, $allowed_pages)) {
    include 'content.php';
} else {
    echo 'Access denied.';
}