Reputation: 4889
just trying to generate a simple XML document using PHP, I found an example which seems to be working, how ever im getting this error:
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.
Here is my PHP code:
$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());
This is what I get if I view my page source:
<?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 -->
So it does seem to generate the XML properly, but the web hosting is adding some crap, is that is what would be causing the error, if so is the only way to stop that to change webhost?
Upvotes: 2
Views: 318
Reputation: 57378
It much depends on how the analytics code gets added. You can try disabling the php_auto_append
directive in .htaccess, or inspect your php.ini values through whatever control panel your hosting is offering. If you normally use the analytics code and the above otherwise works, put your XML/JSON files into a separate folder, ensure that the Apache AllowOverride directive for the virtualHost contains FileInfo
(or it is set to All
) and place your XML/JSON-generating files in that folder together with a .htaccess
php_value "auto_append_file" None
These are, so to speak, the "official/elegant" ways of doing this. Opening a ticket with your hoster is the recommended way to go should this not be enough.
Given that sending analytics code will break all sort of JSON/XML services, it is a fair bet that there is an easy way of disabling it. It may involve placing the file in a specific folder (i.e., the above workaround is actually already in place), or giving it a specific name server side, or something else - but there should be a way.
That said, if nothing else works, you can try to get the client browser to ignore the extra content:
$actualContent = $xml->asXML();
Header('Content-Type: text/xml');
Header('Content-Length: ' . strlen($actualContent));
Header('Connection: close');
die($actualContent);
If the above headers arrive intact to the client (the hoster could mess with them too...), the connection will drop and only the first "valuable" bytes will get considered. THIS IS A HACK, because it relies on undocumented/untested behaviour.
Another similar hack involves sending a multipart header with the first part ending after your $actualContent
is sent, followed by a unhandled MIME-type. Any client expecting XML will extract the first section and ignore the rest. This too is not clean behaviour since the transmission will lack the ending block.
For the same reason, ending the XML in a comment <!--
might work with some browsers.
Upvotes: 2
Reputation: 1952
There are 3 approaches for doing this:
1) There is always in link in their cPanels that disables the analytics code in your code. Most of time the link of file is Your_web_host_url/analytics.php
. But always consider that by doing this you deny their policy and they will eventually delete your account and it causes losing your data.
2) You can use a exit
command at the end of your php script.
3) Create a .htaccess
in your public_html
folder and add the following
code to it:
<FilesMatch "\.(php)$">
php_value auto_append_file none
</FilesMatch>
replace .php
with any type of file that you don't want analytics in it.
Upvotes: 2
Reputation: 1517
Shame on the hosting provider here. For others experiencing the same problem, die()
will stop the execution and thus will not add the analytics code from hosting24.
Upvotes: 2