What are potential security risks associated with allowing user input to determine which PHP file to include?

Allowing user input to determine which PHP file to include can lead to security risks such as directory traversal attacks, remote code execution, and file inclusion vulnerabilities. To mitigate these risks, it is crucial to validate and sanitize user input before using it to include files. One way to do this is by using a whitelist approach, where only specific file paths that are known to be safe are allowed to be included.

$allowed_files = ['file1.php', 'file2.php', 'file3.php'];

$user_input = $_GET['file'];

if (in_array($user_input, $allowed_files)) {
    include($user_input);
} else {
    echo "Invalid file specified";
}