Are there any security considerations to keep in mind when using PHP to include different pages within a single webpage?

When including different pages within a single webpage using PHP, it is important to sanitize user input to prevent potential security vulnerabilities such as code injection attacks. One way to mitigate this risk is to use the "include" or "require" functions with a predefined list of allowed pages, rather than directly including user input.

<?php
$allowed_pages = ['page1.php', 'page2.php', 'page3.php'];
$page = $_GET['page'] ?? 'default.php';

if (in_array($page, $allowed_pages)) {
    include($page);
} else {
    include('error.php');
}
?>