What potential security risks are associated with using user input directly in the include function?

Using user input directly in the include function can lead to security risks such as remote code execution, directory traversal attacks, and file disclosure vulnerabilities. To mitigate these risks, it is important to validate and sanitize user input before using it in the include function. This can be done by checking if the file path is within an allowed directory and restricting access to sensitive system files.

$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 input";
}