What are the potential security risks associated with including pages in PHP using user input like $_GET['layer']?

Including pages in PHP using user input like $_GET['layer'] can lead to security risks such as remote code execution, directory traversal attacks, and potential injection vulnerabilities. To mitigate these risks, it is essential to validate and sanitize user input before including any files.

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

$layer = isset($_GET['layer']) && in_array($_GET['layer'], $allowed_layers) ? $_GET['layer'] : 'default';

include_once($layer . '.php'); // Include the desired page after validating the input