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
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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.