php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #77758 Warning: headers already sent should identify what triggered the send
Submitted: 2019-03-17 22:03 UTC Modified: 2019-03-18 23:17 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: webmaster at jamescobban dot net Assigned:
Status: Open Package: Apache2 related
PHP Version: 7.2.16 OS: linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: webmaster at jamescobban dot net
New email:
PHP Version: OS:

 

 [2019-03-17 22:03 UTC] webmaster at jamescobban dot net
Description:
------------
The message Warning: Cannot modify header information - headers already sent is one of the big time-wasters of our profession.  All of the documentation on this message including hundreds of web-pages and forum discussions end up as just a long list of things that the program MIGHT have done that would cause this message to appear.  There is no documentation of exactly how a programmer should go about identifying exactly what the, usually trivial, mistake was that triggered the early send of the headers.  The message should include information that directly points at the program action that caused PHP to send the headers.

Test script:
---------------
print '';  // empty string
header("Content-Type: application/json");

Expected result:
----------------
Warning: Cannot modify header information - headers already sent because of backtrace to line 1.

Actual result:
--------------
Warning: Cannot modify header information - headers already sent at line 2. 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-17 22:08 UTC] spam2 at rhsoft dot net
> print '';  // empty string
> header("Content-Type: application/json");

just don't echo / print anything ebfore headers, it's that easy

> headers already sent is one of the big time-wasters

just don't output anything when you intend send headers after, easy

> All of the documentation on this message including 
> hundreds of web-pages and forum discussions

just start your application with ob_start() and the problem is forever, easy
 [2019-03-18 23:17 UTC] webmaster at jamescobban dot net
spam2 at rhsoft dot net has pointed out that the problems resulting from accidentally sending text can be avoided either by "just don't echo / print anything ebfore headers" or by wrapping everything in ob_start/ob_clean.  But neither of these help the programmer to FIX the problem, they just hide it.  The problem is that there is no clean easy way to FIND the errant output in any real-life web-site, because there can be MILLIONS of lines of code in a modern web-site, and that code is the work of multiple team members any one of which can make the small mistake.  On the web-site which triggered this suggestion the output came out of a diagnostic routine that was four nested includes deep.  Because PHP does not tell the programmer WHERE the errant print/echo statement is this required slapping hundreds of "if (headers_sent()) print "<p>" . __FILE__ . ' ' __LINE__;" statements throughout the code and then gradually narrowing in to find the offending print.  This is a brute force debugging method which is inappropriate in 2019.  Computers exist to serve humans, not the other way around.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Oct 22 11:01:29 2019 UTC