Are there any best practices for handling remote file inclusion in PHP to prevent exposing sensitive information?

Remote file inclusion in PHP can be a security vulnerability that allows an attacker to include and execute remote files on a server. To prevent exposing sensitive information, it is important to validate user input and only include files from trusted sources. One way to mitigate this risk is to use a whitelist approach, where only specific files or directories are allowed to be included.

$allowed_files = ['file1.php', 'file2.php']; // List of allowed files

if (in_array($_GET['file'], $allowed_files)) {
    include $_GET['file']; // Include the file only if it is in the whitelist
} else {
    // Handle the error or redirect to a safe page
    echo "Invalid file requested.";
}