Jump to content

PHP include XML - Headers already sent?


wwfc_barmy_army

Recommended Posts

Hello. I'm trying to use include to show a XML file (with a XSL stylesheet on) although i get this error:

Warning: Cannot modify header information - headers already sent by (output started at C:\public_html\hotelbookings\index.php: in C:\public_html\hotelbookings\fulllistxml.php on line 2

 

 

The first two lines of the phpfile that generates the XML is :

<?php
header("Content-type: text/xml");

 

Any ideas how i can get around this?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/99371-php-include-xml-headers-already-sent/
Share on other sites

fulllistxml.php:

<?php
header("Content-type: text/xml");

$host = "localhost";
$user = "root";
$pass = "****";
$database = "***";

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");

$query = "SELECT * FROM ****";
$resultID = mysql_query($query, $linkID) or die("Data not found.");

$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output = "<?xml-stylesheet type=\"text/xsl\" href=\"style.xsl\"?>\n";
$xml_output .= "<hotellist>\n";

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
    $row = mysql_fetch_assoc($resultID);
    $xml_output .= "\t<hotel>\n";
...etc....

 

index.php beginning code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Hotel Booking System</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="default.css" rel="stylesheet" type="text/css" />
<script src="livesearch.js"></script> 
<?php include("dbconnect.php"); ?>
</head>
<body>
<div id="header">

 

It is included like:

<?php include("fulllistxml.php"); ?>

 

 

Thanks.

What page includes fulllistxml.php? Basically, if you want the XML to show as an XML file, you can only send the header() and the XML to the screen. At some point, index.php is running, and since it sends output to the screen it will cause it to fail. fulllistxml.php needs to be opened in the browser independent of everything else. Like:

 

<a href="filllistxml.php">View XML</a>

 

Make sense?

you have a couple of options if you want it displayed in-page:

 

-Use my recommendation from earlier, but load it in an iframe instead of a new window

-Print it out the way you have it, but don't send headers, and run the XML through htmlspecialchars() first, so it translates the < and > to < and $gt;

 

...there might be more, but those are the first that come to my head

There is no need to output buffer because he is already storing the XML in a string. But, if you try to do a print $xml_string the browser will see it as HTML (since the content type on the page is text/html). Running it through htmlspecialchars() though will make it print the tags.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.