Are there any security concerns to consider when dynamically loading PHP files based on user input?

When dynamically loading PHP files based on user input, there is a risk of allowing users to include arbitrary files from the server file system, which can lead to security vulnerabilities such as remote code execution. To mitigate this risk, it is important to validate and sanitize user input before using it to include files. One way to do this is to maintain a whitelist of allowed files and only include files that are on the whitelist.

// Validate and sanitize user input
$user_input = $_GET['file'];
$whitelist = ['file1.php', 'file2.php', 'file3.php'];

// Check if the user input is in the whitelist
if (in_array($user_input, $whitelist)) {
    include($user_input);
} else {
    echo "Invalid file.";
}