How can PHP be used to automatically redirect to the index page when accessing specific content pages?

To automatically redirect to the index page when accessing specific content pages in PHP, you can use the header() function to send a raw HTTP header to the browser. You can check if the current page matches the specific content pages and then redirect to the index page using the header("Location: index.php") function.

<?php
// Check if the current page is a specific content page
$current_page = basename($_SERVER['PHP_SELF']);
$specific_pages = array("page1.php", "page2.php", "page3.php");

if (in_array($current_page, $specific_pages)) {
    header("Location: index.php");
    exit();
}
?>