在页面中包含 XML 会导致 PHP 崩溃

发布于 2024-10-04 23:53:50 字数 243 浏览 0 评论 0原文

如果很抱歉,这可能已经在其他地方得到了回答。我在远程服务器上有一个动态 XML 文档,第一行有 行。我猜这会导致 PHP 抛出豁免,因为 ?>

我的问题是如何将此文档包含在我的页面中而不导致 PHP 引发异常?

干杯

编辑:我通过使用 require 来包含,可能不是正确的方法?

This might have been answered somewhere else if so sorry. I have an dynamic XML document on a remote server that has the line <?xml version="1.0" encoding="UTF-8"?> on the first line. This causes PHP to throw an exemption I'm guessing because of the ?>

My question is how do I include this document in my page without causing PHP to throw an exception?

Cheers

EDIT: I'm including by using require, probably not the correct way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

家住魔仙堡 2024-10-11 23:53:50

requireinclude 是错误的工具。是的,两者都包含该页面 - 但随后他们尝试在您的计算机上将其作为 PHP 代码运行,而不采取任何安全措施。想象一下,如果远程计算机返回 会发生什么 - 您计算机上的 PHP 会很乐意尝试关闭您的服务器。

您正在寻找的函数是 readfile() - 它获取远程文件并输出它 - 而不尝试将其作为 PHP 运行。

require and include are the wrong tools for this. Both include the page, yes - but then they attempt to run it as PHP code on your machine, without any security measures. Imagine what would happen if the remote machine returned <?php system('poweroff') ?> - PHP on your machine would happily try to shutdown your server.

The function you're looking for is readfile() - it fetches the remote file and outputs it - without trying to run it as PHP.

痕至 2024-10-11 23:53:50

这听起来像是 short_open_tag 配置选项的问题,该选项会将 解释为 PHP 开始标记。您可能想尝试使用 ini_set() 函数在包含远程文件之前将 short_open_tag 设置为 false,然后再将其切换回来。

编辑:如果您引用的文件/URL 不包含任何需要运行的 PHP 代码,您可以使用 readfile() 函数一口气读取并输出其内容。如果从服务器下载的文件包含 PHP 片段,这将会中断,但如果数据完全准备就绪,则应该可以工作。

This sounds like an issue with the short_open_tag configuration option, which will interpret <? as a PHP opening tag. You may want to try using the ini_set() function to set short_open_tag to false immediately before including the remote file, and then switch it back afterwards.

EDIT: If the file/URL you're referencing doesn't contain any PHP code that needs to be run, you can use the readfile() function to read and output its contents in one fell swoop. This will break if the file, as downloaded from the server, contains PHP snippets, but should work if the data is completely ready to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文