By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,493 Members | 3,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,493 IT Pros & Developers. It's quick & easy.

[FAQ] Why am I getting the error message 'Headers already sent'?

P: n/a
OK, here's my 2p worth:
===

Q. Why am I getting the error message 'Headers already sent'?

A. PHP produces this error message when you try to set a header for a web
page after you have already started sending out the content of the page.

Web content is always delivered with a few headers at the top, ending with a
blank line. For example, a web page might start like this:
HTTP/1.1 200 OK
Date: Tue, 01 Mar 2005 12:00:00 GMT
Content-Type: text/html

<html>
<body> ... etc...
Once you have started sending the content of a document, you can't add any
more headers because they won't appear at the top.

To get round this problem, use PHP's output control functions to buffer the
output before it is sent. For example, you can generate a "Content-Length"
header as follows:
<?php

ob_start(); // Turn on output buffering

// Create your web page/jpeg file/whatever here
echo "<html><body> ... ";

// Generate a "Content-Length" header
$clen = ob_get_length();
header("Content-Length: $clen");

// Now send the buffered content
ob_flush();

?>


--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
A seconary question (off the main list perhaps?)
-----------------------------------------------------

Q. After turning on output buffering I am still getting 'Headers already
sent.' What the?
A. Something, somewhere is sent to the browser prior to the call to
ob_start().

One possible culprit is white-spaces contained in an included file. To fix
this, move the call to ob_start() ahead of any include/require statements.

Another possible culprit is UTF-8 encoding. Unicode-capable editor often
place an invisible character at the beginning of a UTF-8 text file to mark
it as UTF-8. This character will be output before any PHP statements are
executed. To fix this, resave the file as ASCII.
Jul 17 '05 #2

P: n/a
Chung Leong wrote:
A seconary question (off the main list perhaps?)
-----------------------------------------------------

Q. After turning on output buffering I am still getting 'Headers already
sent.' What the?
A. Something, somewhere is sent to the browser prior to the call to
ob_start().

One possible culprit is white-spaces contained in an included file. To fix
this, move the call to ob_start() ahead of any include/require statements.

Another possible culprit is UTF-8 encoding. Unicode-capable editor often
place an invisible character at the beginning of a UTF-8 text file to mark
it as UTF-8. This character will be output before any PHP statements are
executed. To fix this, resave the file as ASCII.


Wouldn't that be throwing the baby out with the bathwater? If I remember
correctly, the invisible character in question is the 'BOM' (Byte Order
Mark), and it's usually an option in Unicode-aware editors to save a
file with or without BOM.

I would advise to try saving without BOM first and only resort to ASCII
if that fails.

JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #3

P: n/a
Chung Leong wrote:
A seconary question (off the main list perhaps?)
-----------------------------------------------------

Q. After turning on output buffering I am still getting 'Headers already
sent.' What the?
A. Something, somewhere is sent to the browser prior to the call to
ob_start().

One possible culprit is white-spaces contained in an included file. To fix
this, move the call to ob_start() ahead of any include/require statements.

Another possible culprit is UTF-8 encoding. Unicode-capable editor often
place an invisible character at the beginning of a UTF-8 text file to mark
it as UTF-8. This character will be output before any PHP statements are
executed. To fix this, resave the file as ASCII.


Good point :-)

I also made a mistake in my example script: "ob_flush()" should be changed
to "ob_end_flush()".

--
phil [dot] ronan @ virgin [dot] net
http://vzone.virgin.net/phil.ronan/
Jul 17 '05 #4

P: n/a

"Jan Pieter Kunst" <de*****@cauce.org> wrote in message
news:42***********************@news.xs4all.nl...
Wouldn't that be throwing the baby out with the bathwater? If I remember
correctly, the invisible character in question is the 'BOM' (Byte Order
Mark), and it's usually an option in Unicode-aware editors to save a
file with or without BOM.

I would advise to try saving without BOM first and only resort to ASCII
if that fails.


Consider the audience though. The likely scenario is someone editting in
Notepad and accidently saving the file as UTF-8.

The character in question is the zero-width non-breaking space (U+FFEF). In
UTF-16 text it's used as a byte order indicator. In UTF-8 it's just a
signature. If you leave it out then an editor might not be able to correctly
sniff out the encoding, leaving to other problems.
Jul 17 '05 #5

P: n/a
Philip Ronan <in*****@invalid.invalid> wrote:
Q. Why am I getting the error message 'Headers already sent'?

[ A. the explanation why and how to counter the symptoms]

If you get this error your script flow is broken (in most cases). Using
OB is nothing more than hiding the symptoms of the error.

The error tells you where the real problem lies:
1:<?php
2:error_reporting(E_ALL);
3://do stuff
4:echo "redirecting";
5:
6://do more stuff
7:
8:header("Location: http://localhost/");
9:?>

will produce the error:

Warning: Cannot modify header information - headers already sent by
(output started at /path/to/script.php:4) in /path/to/script.php on line 8

What is really is trying to say:

Error: line 8 at /path/to/script.php can't send headers. The problem is
at line 4 in /path/to/script.php, it produced some output to the client
so I already had sent all headers before getting to line 8.

But since you already have all required userinput you should find out
wheter you need to sent additional headers (like a redirect) before
outputting anything to the client. So the equivalent script with correct
top down flow would be:
1:<?php
2:error_reporting(E_ALL);
3:if($condition){
4:header("Location: http://localhost/");
5:die("redirecting");
6:}
7:
8:echo "condition was false";
9:?>
The following script:
1:<?php
2://do stuff
3:?>
4:<html><body>
5:Redirecting
6:</body></html>
7:<?php
8:header("Location: http://tmp.tryba.nl/");
9:?>

Produces:

Warning: Cannot modify header information - headers already sent by
(output started at /path/to/script.php:7) in /path/to/script.php on line 8

Now it complains about line 7 since the <?php line 7 apparently flushed
the stringbuffer that got build between lines 3 and 7.

When including file the error could look like this:

Warning: Cannot modify header information - headers already sent by
(output started at /path/to/other.php:5) in /path/to/script.php on line 6

Now it is other.php which produces output to the browser at line 5, so
line 6 in script.php can't modify headers any longer.

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.