Are there any potential security risks associated with automatically displaying subpages of a website in PHP?

Automatically displaying subpages of a website in PHP can pose security risks such as exposing sensitive information or allowing unauthorized access to certain pages. To mitigate these risks, it is important to properly sanitize user input, validate permissions before displaying subpages, and implement proper error handling to prevent potential exploits.

// Example code snippet to display subpages securely
$page = $_GET['page'];

$allowed_pages = ['page1', 'page2', 'page3']; // Define an array of allowed subpages

if (in_array($page, $allowed_pages)) {
    include($page . '.php'); // Include the requested subpage if it is allowed
} else {
    echo 'Page not found'; // Display an error message if the subpage is not allowed
}