472,371 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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

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
5 2412
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: R. Rajesh Jeba Anbiah | last post by:
----------------------------------------------------------------- This is the FAQ thread where the FAQ compilation project goes. * If you wish to improve the contents, please copy the whole...
0
by: Janwillem Borleffs | last post by:
----------------------------------------------------------------- This is the FAQ thread where the FAQ compilation project goes. * If you wish to improve the contents, please copy the whole...
0
by: Janwillem Borleffs | last post by:
----------------------------------------------------------------- This is the FAQ thread where the FAQ compilation project goes. * If you wish to improve the contents, please copy the whole...
0
by: Janwillem Borleffs | last post by:
----------------------------------------------------------------- This is the FAQ thread where the FAQ compilation project goes. * If you wish to improve the contents, please copy the whole...
3
by: R. Rajesh Jeba Anbiah | last post by:
----------------------------------------------------------------- This is the FAQ thread where the FAQ compilation project goes. * If you wish to improve the contents, please copy the whole...
4
by: Richard Cornford | last post by:
For the last couple of months I have been trying to get the next round of updates to the FAQ underway and been being thwarted by a heavy workload (the project I am working on has to be finished an...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.