PHP Header Question

Jpitzo

Limp Gawd
Joined
May 31, 2002
Messages
453
this may be a n00b question, but I recently started my own weblog and I'm having problems with the comment section. Basically, I want it so when a user posts a comment it checks the form, submits it, and then redirects the user to the actual comment page. I have everything working except it gives me this error when it tries to redirect:

Warning: Cannot modify header information - headers already sent by (output started at /home/content/j/p/i/jpitzo/html/php/comments/postcomm.php:11) in /home/content/j/p/i/jpitzo/html/php/comments/postcomm.php on line 36

Code:
<?php
// If form is filled out and submitted, POST form
if (isset($_POST['post'], $_POST['title'])) {
	
---DATABASE STUFF HERE---
	
	header('Location: [url]http://www.joeyp.net/php/comments/compage.php?comname=[/url] ' . $comname );
}				
else {	
//If form isnt filled out, display form	
		echo "<form action='" . $postcomname . "' method='post'>
		Title: <input type='text' name='title' /><br /><br />
        Entry: <textarea name='post' rows='15' cols='20'>
  		</textarea><br />
  		Name: <input type='text' name='name' /><br /><br />
        <input type='submit' />
        </form>";
}
?>
other stuff here...
 
hmm...theres no whitespace, although there are html tags above and below that php block. Is that the problem. And if so how would I get around it.
 
the header function is only looking for a string...so do this:
PHP:
header("Location: http://www.joeyp.net/php/comments/compage.php?comname=$comname")

double quotes will allow the use of variables without having to concatonate onto the end.
 
Jpitzo said:
hmm...theres no whitespace, although there are html tags above and below that php block. Is that the problem. And if so how would I get around it.

That is the problem. When changing header data from within a script, it has to be changed before any output is sent to the browser. This would commonly be done with html, regular text, or even white space. It also applies to anything sent by PHP, including your echo's. The way around this would be to use PHP's Output Control. The main function to follow, in an example, would be;

Code:
<?php

// Start catching all output before it is sent
ob_start();

// send some output
echo 'Test';

// Modify headers
header("Cache-Control: no-cache, must-revalidate");

// More output
echo '<strong>completed</strong>.';

// Finally send all the output to the client
ob_end_flush();

?>
 
yes! that was exactly what I was looking for. works like a charm. thanks!
 
Back
Top