473,288 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 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 2499
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.