473,718 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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><bod y> ... ";

// 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 2538
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*****@invali d.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_reporti ng(E_ALL);
3://do stuff
4:echo "redirectin g";
5:
6://do more stuff
7:
8:header("Locat ion: 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_reporti ng(E_ALL);
3:if($condition ){
4:header("Locat ion: http://localhost/");
5:die("redirect ing");
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("Locat ion: 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
1429
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 content, fix it and then post it. When posting, please change the revision number (increase by 1) in the subject line. * If you want to comment, do it without changing the subject line. * Do NOT add new question and answers here. Add here *only* after...
0
1502
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 content, fix it and then post it. When posting, please change the revision number (increase by 1) in the subject line. * If you want to comment, do it without changing the subject line. * Do NOT add new question and answers here. Add here only after...
0
1303
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 content, fix it and then post it. When posting, please change the revision number (increase by 1) in the subject line. * If you want to comment, do it without changing the subject line. * Do NOT add new question and answers here. Add here only after...
0
1611
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 content, fix it and then post it. When posting, please change the revision number (increase by 1) in the subject line. * If you want to comment, do it without changing the subject line. * Do NOT add new question and answers here. Add here only after...
3
2258
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 content, fix it and then post it. When posting, please change the revision number (increase by 1) in the subject line. * If you want to comment, do it without changing the subject line. * Do NOT add new question and answers here. Add here only after...
4
2633
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 QA tested for a new year release. I don't think that going to prove practical, but there is no harm in trying :) and some serious family commitments. But it has to be done soon so this is stage one. Mike Winter provided an extensive list of...
0
8827
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9053
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6654
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5971
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4481
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4743
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3182
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2555
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.