Page 1 of 1

Blog tutorial - Problem with comments

Posted: Thu Jun 23, 2011 10:36 am
by nyo
Hi,

I am on the last video of the blog tutorial series and everything works fine up to now. I am at the comments section. When I submit a comment, it gives an error although the comment is added to the database:

"Warning: Cannot modify header information - headers already sent by (output started at C:\Apache2\htdocs\web\post.php:1) in C:\Apache2\htdocs\web\post.php on line 7"

Here is the related part of my code and I can't see what's wrong with it:
<?php include 'adm/inc/header.php';
include 'adm/inc/posts.php';
include 'adm/inc/comments.php';
		   
if (isset($_GET['pid'], $_POST['user'], $_POST['body'])) {
	if(add_comment($_GET['pid'], $_POST['user'], $_POST['body'])) {
		header("Location: post.php?pid={$_GET['pid']}");
	} else {
		header('Location: index.php');
	}
	
	die();
}
?>
Thanks.

Re: Blog tutorial - Problem with comments

Posted: Thu Jun 23, 2011 10:54 am
by jacek
Headers have to come before any output from the script, just because that's how HTTP works (headers before body). From the looks of it there is nothing before the header lines but can you confirm that there is no output (including spaces) before the <?php tag ?

Re: Blog tutorial - Problem with comments

Posted: Thu Jun 23, 2011 11:08 am
by nyo
jacek wrote:Headers have to come before any output from the script, just because that's how HTTP works (headers before body). From the looks of it there is nothing before the header lines but can you confirm that there is no output (including spaces) before the <?php tag ?
There is nothing before the first <?php tag in the post.php file. But I have header.php file different from your tutorial, I guess I will start from scratch and use the same files as you.

Re: Blog tutorial - Problem with comments

Posted: Thu Jun 23, 2011 1:41 pm
by jacek
nailyener wrote:I guess I will start from scratch
I never understood why people jump straight to starting again.

Does your editor have an option to save the file without a BOM (google for what it actually is)

Re: Blog tutorial - Problem with comments

Posted: Thu Jun 23, 2011 2:12 pm
by bowersbros
jacek wrote:
nailyener wrote:I guess I will start from scratch
I never understood why people jump straight to starting again.

Does your editor have an option to save the file without a BOM (google for what it actually is)
I just googled it as i had never heared of it before.. turns out to be a drug? ;)

Nah just kidding.. something about bytes.. But i dont understand how that setting would affect it if you have nothing before the <?php

Re: Blog tutorial - Problem with comments

Posted: Thu Jun 23, 2011 2:49 pm
by jacek
Because when you save with that there it adds a single byte to the start of the file.

Re: Blog tutorial - Problem with comments

Posted: Sat Jun 25, 2011 9:18 am
by nyo
jacek wrote:I never understood why people jump straight to starting again.

Does your editor have an option to save the file without a BOM (google for what it actually is)
Anyway, starting from scratch didn't work either. I have everything same as you: code, filenames etc. but still getting the same warning when I add a comment:

"Warning: Cannot modify header information - headers already sent by".

The comment is added to the database though.

I saved the files in "UTF-8 without BOM" format but it didn't help either.

UPDATE: I am not really sure what fixed the problem but it is solved now. I guess it is probably creating the files again in the "UTF-8 without BOM" format. Thanks for your input.