只是尝试使用 PHP 生成一个简单的 XML 文档,我发现了一个似乎有效的示例,我怎么会收到此错误:
This page contains the following errors:
error on line 5 at column 1: Extra content at the end of the document Below is a rendering of the page up to the first error.
这是我的 PHP 代码:
$xml = new SimpleXMLElement("<xml/>");
$form_data = array(
"name" => $_POST['name'],
"email" => $_POST['email'],
"message" => $_POST['message'],
"reg" => $_POST['reg'],
"image" => $image_location
);
$xml->addChild('uploaded',serialize($_POST));
$xml->addChild('imageerror',"imageerror");
header('Content-type: text/xml');
print($xml->asXML());
如果我查看我的页面源代码,这是我得到的:
<?xml version="1.0"?>
<xml><uploaded>a:0:{}</uploaded><imageerror>imageerror</imageerror></xml>
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
所以它似乎确实正确地生成了 XML,但是虚拟主机添加了一些废话,这就是导致错误的原因吗?如果是这样,唯一的方法就是停止更改虚拟主机?
最佳答案
有 3 种方法可以做到这一点:
1) 他们的 cPanel 中总是有一个链接,可以禁用您代码中的分析代码。大多数时候文件的链接是 Your_web_host_url/analytics.php
。但始终要考虑到,这样做你就是在否认他们的政策,他们最终会删除你的帐户,这会导致你的数据丢失。
2) 您可以在 php 脚本末尾使用 exit
命令。
3) 在您的 public_html
文件夹中创建一个 .htaccess
并添加以下内容
代码:
<FilesMatch "\.(php)$">
php_value auto_append_file none
</FilesMatch>
将 .php
替换为您不想在其中进行分析的任何类型的文件。
关于PHP生成XML,获取文档末尾的Extra内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32553613/