Jump to content

Cannot Modify Header Information


lanmind

Recommended Posts

Hello,I was trying to output XML with my PHP when I came across this warning:Warning: Cannot modify header information - headers already sent by (output started at /home/content/l/a/n/lanmind/html/lanmindphp.php:33) in /home/content/l/a/n/lanmind/html/lanmindphp.php on line 49I've looked into this googling it and I've concluded that I have unnecessary blank spaces in my code or when I use the header() function it's after output has started. I didn't find any blank spaces around line 33 or at the beginning or end of the script. I've used the header() function in another script before and didn't have this problem. I also have a error telling me the "xmldoc" is null onkeyup in the second text box here: http://www.lanmind.net/I'm not sure if this warning is fatal so PHP isn't outputting the XML or if I have some syntax wrong in the PHP here:

<?php	// Start the session for this PHP script	// The session was started earlier on the user's browser in Index.php	session_start();		function parseToXML($htmlStr)	{ 		$xmlStr=str_replace('<','<',$htmlStr);		$xmlStr=str_replace('>','>',$xmlStr); 		$xmlStr=str_replace('"','"',$xmlStr); 		$xmlStr=str_replace("'",''',$xmlStr); 		$xmlStr=str_replace("&",'&',$xmlStr); 		return $xmlStr; 	}	require('currentlanmind_dbinfo.php');	// Opens a connection to a MySQL server	$connection= mysql_connect ($hostname, $username, $password);	if (!$connection)	{		die('Not connected : ' . mysql_error());	}		// Set the active MySQL database	$db_selected = mysql_select_db($database, $connection);	if (!$db_selected)	{		die ('Can\'t use db : ' . mysql_error());	}		// Put the passed user data from the Question form into a variable	$question = mysql_real_escape_string($_POST['name'], $connection);		// If the user data was not set in the $question var then put the user's answer form data into a var	if(!$question)	{		// Put the passed user data from the Answer form into the var $userinput		$userinput = mysql_real_escape_string($_POST['useranswer'], $connection);				// In the db's answers table update the answer column where the id equals $_SESSION['selectedid'] with the user's input		// The $_SESSION['selectedid'] was set with the selected question's id when the user submited the question form		mysql_query("UPDATE answers SET answer= '$userinput' WHERE id='{$_SESSION['selectedid']}'");				//Retrieve the user's original question from the answers table and get the answer!		$originalquestion= mysql_query("SELECT question, answer, id from answers WHERE id='{$_SESSION['userid']}'");						header("Content-type: text/xml");				// Echo Parent Node		echo '<response>';				// Start Child Nodes with Responses		echo '<results>';				// Iterate through the rows, printing XML nodes for each		while ($row = @mysql_fetch_assoc($originalquestion))		{		  // ADD TO XML DOCUMENT NODE		  echo '<result ';		  echo 'question="' . parseToXML($row['qusetion']) . '" ';		  echo 'answer="' . parseToXML($row['answer']) . '" ';		  echo 'id="' . parseToXML($row['id']) . '" ';		  echo '/>';		}		//End Responses		echo '</results>';				// End XML file		echo '</response>';	}		// If the user data was set in the $question var then continue on with code	else	{		// Insert a new record into the db's questions table with the question form's submited user data in the question column.		// Each row gets a new Id with the auto_increment attribute and a Timestamp for user's to SELECT the oldest record.		mysql_query("INSERT INTO questions (question) VALUES ('$question')");				// Using PHP's mysql_insert_id() function store the user's last created id as session data		$_SESSION['userid']= mysql_insert_id();				// Store a query that selects the oldest record in the questions table in the $result var		// Maybe add a subquery here that adds a value to a "check column" so the oldest record isn't selected by two user's!		$result = mysql_query("SELECT question,id from questions ORDER BY TIMESTAMP ASC LIMIT 1");				// Using PHP's function mysql_fetch_array() set the var $row to an array of the var $result		// the var $result is a query that selects the oldest record in the questions table		$row = mysql_fetch_array($result);				// Set the var $questionid as the var $row's array id parameter		$questionid= $row['id'];				//Move the selected record's id and question columns into the answers table by using the $questionid var in the WHERE clause		mysql_query("INSERT INTO answers (id,question) SELECT id,question FROM questions WHERE id='$questionid'");				// store the selected qusetion's id as session data		$_SESSION['selectedid']= $questionid;				// Delete from the questions table the oldest record selected before another user selects it!		// Need to perform this DELETE using the var selectedid before assigning the questionid as the SESSION['selectedid']		mysql_query("DELETE FROM questions WHERE id='{$_SESSION['selectedid']}'");				echo "Selected question: ". $row['question'];		echo "<br />" . "<br />";		echo "Question Id AKA Selected Id: " . $questionid;		echo "<br />" . "<br />";		echo "<br />" . "<br />";		echo "User Id: " . $_SESSION['userid'];		echo "<br />" . "<br />";		echo "User Input: " . stripslashes($question);	}?>

Thank you for your time.

Link to comment
Share on other sites

Warnings aren't fatal. You do have a syntax error on this line though:$xmlStr=str_replace("'",''',$xmlStr); Unless that's getting messed up in the forum somehow, it looks like the lines above and below that are using entity names.Line 33 looks like a call to mysql_real_escape_string. That's only going to output something if there's a problem like the database connection is invalid. If it was though, it wouldn't even make it to that line. It does look like you're using a variable from post on line 33, if that variable is not set in post and you have error reporting high enough, that will output a notice telling you the variable isn't set and that would be the output that breaks header.

Link to comment
Share on other sites

Yes I looked some more and I don't know what's going on. It even got to the point that instead of updating the page with the Ajax it was loading lanmindphp.php. I removed all code that I created when attempting to output the XML and everything is functioning ok again just using PHP's .responseText. I will try to output XML again later.

Link to comment
Share on other sites

I've figured some things out about this header problem. In my PHP I have a variable definition that doesn't get defined so the PHP returns:Notice: Undefined index: name in /home/content/l/a/n/lanmind/html/lanmindphp.php on line 33.This notice is returned first before I use the header() function to start the XML output: header("Content-type: text/xml"); The warning again is:Warning: Cannot modify header information - headers already sent by (output started at /home/content/l/a/n/lanmind/html/lanmindphp.php:33) in /home/content/l/a/n/lanmind/html/lanmindphp.php on line 48Is this notice return the output generating the warning? I've read this:http://www.kirupa.com/forum/showpost.php?p...amp;postcount=3Thank you for your time.

Link to comment
Share on other sites

Yes this was the problem. I stopped the Notice from being outputted by prepending line 33's expression with "@". Anybody know of a way to call seperate functions in PHP based upon two seperate HTML forms? I believe the easiest way is to make two seperate PHP scripts to handle each but I'd like to keep all my PHP in one script.I was thinking something like:if isset($_POST['name']){ function code...}else{ var= $_POST['age'] function code...}Just not sure if this is possible.

Link to comment
Share on other sites

So I'm outputting XML and I'm trying to output XML continuable in a loop while a condition is true. Here is what I have:

$originalquestion= mysql_query("SELECT question, answer, id from answers WHERE id='{$_SESSION['userid']}'");		$row = mysql_fetch_array($originalquestion);				while ($row['id']<=500)		{			header("Content-type: text/xml");			echo '<person>';			echo "<result>" . $row['id'] . "</result>";			echo "</person>";		}

When this script is run I'm getting a "junk after document element" error. I believe since a new header is being started after the XML root element's ending tag it's generating this error. I've never tried looping through output like this and I don't know if it's possible. I tried using PHP's flush() function in combination with the time_nanosleep() function to send the XML output then start a new output but it didn't work. Is it possible for PHP to keep outputting DB query results?

Link to comment
Share on other sites

Headers only need to be sent once; they apply to the entire document. In your code the header calls are after output, and so are generating the notice. This is junk, and so the browser's parser gives an error.You can only call header() before ANY output is sent.

Link to comment
Share on other sites

Hello,Do you mean the second time it loops though the code another header is being called after the output of the first loop and that generates an error? Because the first loop is fine not generating any notices or errors.

Link to comment
Share on other sites

No, sending even a blank line or a space will cause the html pae to submit a Header, so check your script and all included files to see if a blank line is being sent early.

Link to comment
Share on other sites

Do you mean the second time it loops though the code another header is being called after the output of the first loop and that generates an error?
Right. A document has 1 header, not 500 headers throughout the data. Also, that while loop isn't going to end because you're never changing $row inside the loop.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...