Blog tutorial - Problem with comments

Post here is you are having problems with any of the tutorials.
Post Reply
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Blog tutorial - Problem with comments

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog tutorial - Problem with comments

Post 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 ?
Image
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Blog tutorial - Problem with comments

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog tutorial - Problem with comments

Post 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)
Image
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: Blog tutorial - Problem with comments

Post 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
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Blog tutorial - Problem with comments

Post by jacek »

Because when you save with that there it adds a single byte to the start of the file.
Image
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Blog tutorial - Problem with comments

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