What measures can be taken to filter out potentially harmful content when including external files in PHP?

When including external files in PHP, it is important to filter out potentially harmful content to prevent security vulnerabilities such as code injection or execution. One way to do this is by using the `realpath()` function to get the absolute path of the included file and then comparing it to a whitelist of allowed paths. This can help ensure that only safe and trusted files are included in your PHP script.

$allowed_paths = ['/path/to/allowed/file1.php', '/path/to/allowed/file2.php'];
$included_file = '/path/to/external/file.php';

$real_path = realpath($included_file);

if (in_array($real_path, $allowed_paths)) {
    include $real_path;
} else {
    // Handle error or log unauthorized access
}