Skip to main content
Inspiring
September 15, 2008
Question

Dreaded "headers already sent" error

  • September 15, 2008
  • 15 replies
  • 979 views
I'm getting a "headers already sent" error and I can't figure out why.
I have checked for and eliminated all whitespace in my php include files
and still I'm getting the error:

> Warning: Cannot modify header information - headers already sent by
> (output started at
> /Users/brett/vhosts/modules/admin/Products/add_div.inc.php:51)
> in /Users/brett/vhosts/modules/admin/Products/update_grp.inc.php on
> line 44


After updating a table record, I am trying to redirect to a page
containing accordion panels populated with table LIST pages. Here is my
redirect:

>
> if ($Result1) {
> header('Location: http://' . $_SERVER['HTTP_HOST'] .
> dirname($_SERVER['PHP_SELF']) .
> '/admin.php?content=Products/seeSelections');
> exit;
> }


I have looked at the include file listed in the error message and can
find no problem. Here is the code:

> <?php
> if (!function_exists("GetSQLValueString")) {
> function GetSQLValueString($theValue, $theType, $theDefinedValue = "",
> $theNotDefinedValue = "")
> {
> $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) :
> $theValue;
>
> $theValue = function_exists("mysql_real_escape_string") ?
> mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
>
> switch ($theType) {
> case "text":
> $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
> break;
> case "long":
> case "int":
> $theValue = ($theValue != "") ? intval($theValue) : "NULL";
> break;
> case "double":
> $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'"
> : "NULL";
> break;
> case "date":
> $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
> break;
> case "defined":
> $theValue = ($theValue != "") ? $theDefinedValue :
> $theNotDefinedValue;
> break;
> }
> return $theValue;
> }
> }
>
> $editFormAction = $_SERVER['PHP_SELF'];
> if (isset($_SERVER['QUERY_STRING'])) {
> $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
> }
>
> if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "addDiv")) {
> $insertSQL = sprintf("INSERT INTO divisions (div_name) VALUES (%s)",
> GetSQLValueString($_POST['div_name'], "text"));
>
> mysql_select_db($database_testadmin, $testadmin);
> $Result1 = mysql_query($insertSQL, $testadmin) or die(mysql_error());
> }
> ?>
> <form id="addDiv" name="addDiv" method="POST" action="<?php echo
> $editFormAction; ?>">
> <label for="div_name">Name:</label>
> <br />
> <input type="text" name="div_name" id="div_name" />
> <br /><br />
> <input type="submit" name="submit" id="submit" value="Add entry" />
> <input name="MM_insert" type="hidden" id="MM_insert" value="addDiv" />
> </form>

Line 51 in "add_div.inc.php" is the last line shown above - </form> -
and there is no whitespace after the closing >.
Line 44 in "update_grp.inc.php" is the header redirect shown above.
What could be the problem?

TIA

Brett

This topic has been closed for replies.

15 replies

Inspiring
September 15, 2008
Brett wrote:
> I'm completely stumped.

The problem is here:

>> ?>
>> <form action="<?php echo $editFormAction; ?>" id="editGrp"
>> name="editGrp" method="POST">
>> <label for="grp_name">Name:</label>
>> <br />
>> <input name="grp_name" type="text" id="grp_name" value="<?php echo
>> htmlentities($row_getGrp['grp_name'], ENT_COMPAT, 'UTF-8'); ?>" />
>> <br /><br />
>> <input type="submit" name="submit" id="submit" value="Update entry" />
>> <input name="grp_id" type="hidden" id="grp_id" value="<?php echo
>> $row_getGrp['grp_id']; ?>" />
>> <input type="hidden" name="MM_update" value="editGrp" />
>> </form>

The form is HTML output. It doesn't matter that the header() function is
on line 44 ahead of this output. The reason it triggers "headers already
sent" is because the form is part of the include file. There must be no
output to the browser in an include file if you're using header() or
sessions.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
September 15, 2008
David,

I'm completely stumped. I've looked at the code for
"update_grp.inc.php" and I can see no place that the header has been
sent before. Here is the complete code for that page:

> <?php
> if (!function_exists("GetSQLValueString")) {
> function GetSQLValueString($theValue, $theType, $theDefinedValue = "",
> $theNotDefinedValue = "")
> {
> $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) :
> $theValue;
>
> $theValue = function_exists("mysql_real_escape_string") ?
> mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
>
> switch ($theType) {
> case "text":
> $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
> break;
> case "long":
> case "int":
> $theValue = ($theValue != "") ? intval($theValue) : "NULL";
> break;
> case "double":
> $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'"
> : "NULL";
> break;
> case "date":
> $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
> break;
> case "defined":
> $theValue = ($theValue != "") ? $theDefinedValue :
> $theNotDefinedValue;
> break;
> }
> return $theValue;
> }
> }
>
> $editFormAction = $_SERVER['PHP_SELF'];
> if (isset($_SERVER['QUERY_STRING'])) {
> $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
> }
> if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "editGrp")) {
> $updateSQL = sprintf("UPDATE grp SET grp_name=%s WHERE grp_id=%s",
> GetSQLValueString($_POST['grp_name'], "text"),
> GetSQLValueString($_POST['grp_id'], "int"));
>
> mysql_select_db($database_testadmin, $testadmin);
> $Result1 = mysql_query($updateSQL, $testadmin) or die(mysql_error());
>
> if ($Result1) {
> header('Location: http://' . $_SERVER['HTTP_HOST'] .
> dirname($_SERVER['PHP_SELF']) .
> '/admin.php?content=Products/seeSelections');
> exit;
> }
> }
>
> $colname_getGrp = "-1";
> if (isset($_GET['grp_id'])) {
> $colname_getGrp = $_GET['grp_id'];
> }
> mysql_select_db($database_testadmin, $testadmin);
> $query_getGrp = sprintf("SELECT * FROM grp WHERE grp_id = %s",
> GetSQLValueString($colname_getGrp, "int"));
> $getGrp = mysql_query($query_getGrp, $testadmin) or die(mysql_error());
> $row_getGrp = mysql_fetch_assoc($getGrp);
> $totalRows_getGrp = mysql_num_rows($getGrp);
>
> ?>
> <form action="<?php echo $editFormAction; ?>" id="editGrp"
> name="editGrp" method="POST">
> <label for="grp_name">Name:</label>
> <br />
> <input name="grp_name" type="text" id="grp_name" value="<?php echo
> htmlentities($row_getGrp['grp_name'], ENT_COMPAT, 'UTF-8'); ?>" />
> <br /><br />
> <input type="submit" name="submit" id="submit" value="Update entry" />
> <input name="grp_id" type="hidden" id="grp_id" value="<?php echo
> $row_getGrp['grp_id']; ?>" />
> <input type="hidden" name="MM_update" value="editGrp" />
> </form>
>
> <?php
> mysql_free_result($getGrp);
> ?>
As I mentioned before, line 44 starts with "header(Location: ...etc.)
Can you spot the problem? I checked the Page properties on all pages
which are included and none have the BOM selected. Any other thoughts?

Many thanks for your help.

Brett
Inspiring
September 15, 2008
Brett wrote:
> but I don't see where the problem is. Line 44 starts with:
> header('Location: ...etc. as shown above. I have used this code on many
> other pages and it works fine, why not here?

As always with PHP error messages, you need to start working backwards
from that point. One thing I would suggest looking for is the use of the
Byte Order Mark. In each page, select Modify > Page Properties. In the
Title/Encoding section, make sure that Include Unicode Signature (BOM)
is deselected.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
Inspiring
September 15, 2008
Thanks David, I looked at line 44:

> if ($Result1) {
> header('Location: http://' . $_SERVER['HTTP_HOST'] .
> dirname($_SERVER['PHP_SELF']) .
> '/admin.php?content=Products/seeSelections');
> exit;
> }

but I don't see where the problem is. Line 44 starts with:
header('Location: ...etc. as shown above. I have used this code on many
other pages and it works fine, why not here?

Best,

Brett
Inspiring
September 15, 2008
Brett wrote:
> I'm getting a "headers already sent" error and I can't figure out why.
> I have checked for and eliminated all whitespace in my php include files
> and still I'm getting the error:
>
>> Warning: Cannot modify header information - headers already sent by
>> (output started at
>> /Users/brett/vhosts/modules/admin/Products/add_div.inc.php:51)
>> in /Users/brett/vhosts/modules/admin/Products/update_grp.inc.php on
>> line 44

The error message says that output was started on line 44 of
update_grp.inc.php. That's where you should look for the problem.

--
David Powers, Adobe Community Expert
Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/