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();
}
?>
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 ?
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.
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.