The Darn Header Problem

Why is this wrong? Why do I get the darn Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\Yank\add_stuff.html.php:16) in C:\xampp\htdocs\Yank\index.php on line 24?

Here’s add_stuff.html.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="?" method="post">
DJUR:
<input name="djur" type="text" id="djur" />
PARK:
<input name="park" type="text" id="park" /><br />
<input name="knapp" type="submit" id="knapp" value="Lägg till!" />
</form>
</body>
</html>

And here’s index.php:

<?php
$link = mysqli_connect('localhost', 'root', 'xxxxx');
if (!mysqli_select_db($link, 'djur'))
{
$error = 'Unable to locate the animal database.';
include 'error.html.php';
exit();
}
if (!isset($_POST['djur']))
{
include 'add_stuff.html.php';

$djur = mysqli_real_escape_string($link, $_POST['djur']);
$park = mysqli_real_escape_string($link, $_POST['park']);
$sql = 'INSERT INTO olika_djur SET
djur="' . $_POST['djur'] . '",
park="' . $_POST['park'] . '"';
if (!mysqli_query($link, $sql))
{
$error = 'Error adding submitted joke: ' . mysqli_error($link);
include 'error.html.php';
exit();
}
header('Location: .');
exit();
}
?>

In order to fix this issue, you must do all your PHP logic before any HTML is out putted to the browser.

Meaning do any include / print / POST statements before any HTML.

I understand THAT, but what is it inte code that “is off”?

You get error because you have sent output to the browser before making that call. You can’t set headers after output has been sent to the browser.

The reason you are getting this error is because your order of logic is off.

In order to fix this issue, you must do all your PHP logic before any HTML is out putted to the browser.

If you have any doubts about what output might have been sent, then try this simple debugging test:

Comment out your header(), and echo something like “Redirecting to xxx.php”
And then see what output you get when executing the script… remember to look at the source code in the browser, to see if any HTML have been outputtet before the “Redirecting to xxx.php”.

If there are anything before the “Redirecting to xxx.php”, you must change your script.

Example of how you could test it:

if (!isset($_POST['djur']))
{
   include 'add_stuff.html.php';
   exit();
}
 
include 'connect.php';
 
$djur = mysqli_real_escape_string($link, $_POST['djur']);
$park = mysqli_real_escape_string($link, $_POST['park']);
$sql = 'INSERT INTO olika_djur SET
   djur="' . $_POST['djur'] . '",
   park="' . $_POST['park'] . '"';
if (!mysqli_query($link, $sql))
{
   $error = 'Error adding submitted joke: ' . mysqli_error($link);
   include 'error.html.php';
   exit();
}
//header('Location: .');
echo "Redirecting to .";
exit();

Any other output than “Redirecting to .” means you have to change your script.

Try the above change, comeback here and post the resulting source code from the browser.

If you already send ANY output to the browser, you will get that error message.

In this case, the problem was caused by including the ‘add_stuff.html.php’ file (which outputs all that html code immediately) and then trying to redirect.

Hmm… but does not my use of the exclamation change that?

if (!isset($_POST['djur']))

I mean, if you look at the last post with the code that works, I am still including the add.stuff file after the if statement. There must be something else that’s causing the error message?!

Because you are now checking for POST before any HTML is sent to the browser, how it should be.

If I rearrange it like this, it works:

if (!isset($_POST['djur']))
{
include 'add_stuff.html.php';
exit();
}

include 'connect.php';

$djur = mysqli_real_escape_string($link, $_POST['djur']);
$park = mysqli_real_escape_string($link, $_POST['park']);
$sql = 'INSERT INTO olika_djur SET
djur="' . $_POST['djur'] . '",
park="' . $_POST['park'] . '"';
if (!mysqli_query($link, $sql))
{
$error = 'Error adding submitted joke: ' . mysqli_error($link);
include 'error.html.php';
exit();
}
header('Location: .');
exit();

I only wish I could understand why…

Thank you Cerium, but I think Oddz already said that. My question was if somebody could kindly point out where in the code I do this forbidden output.

You problem may be coming from this part


header('Location: .');

You are out putting HTML (sending the HTTP Header) and THEN trying to set additional header information by trying to redirect the user.