What are the potential security risks associated with allowing user input to directly impact file inclusion in PHP?

Allowing user input to directly impact file inclusion in PHP can lead to security risks such as remote code execution, directory traversal attacks, and unauthorized access to sensitive files on the server. To mitigate these risks, it is important to sanitize and validate user input before using it in file inclusion functions.

$user_input = $_GET['file'];

// Validate and sanitize user input
$allowed_files = ['file1.php', 'file2.php', 'file3.php'];
if (in_array($user_input, $allowed_files)) {
    include($user_input);
} else {
    echo "Invalid file requested";
}