What potential security risks are associated with allowing users to access template files directly via URL parameters in PHP?

Allowing users to access template files directly via URL parameters in PHP can pose security risks such as exposing sensitive information, allowing unauthorized access to files, and potentially executing malicious code. To mitigate these risks, it is recommended to validate user input and restrict access to template files by implementing proper file path checks and permissions.

<?php
// Example of restricting access to template files
$allowed_templates = ['template1.php', 'template2.php', 'template3.php']; // List of allowed template files

$template = isset($_GET['template']) ? $_GET['template'] : 'default.php'; // Default template file

if (in_array($template, $allowed_templates)) {
    include($template); // Include the template file
} else {
    // Handle unauthorized access or display an error message
    echo 'Unauthorized access';
}
?>