|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #42197 header() cannot be used after echo on CLI
Submitted: 2007-08-03 09:53 UTC Modified: 2015-01-12 20:30 UTC
Avg. Score:4.5 ± 0.8
Reproduced:24 of 24 (100.0%)
Same Version:2 (8.3%)
Same OS:16 (66.7%)
From: Assigned:
Status: Open Package: *General Issues
PHP Version: 5.2.4RC1 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-08-03 09:53 UTC]
When issuing a header() call in a php command line script (php cli) after I used 'echo', the message 'headers already sent' pops up.

It should be possible to use header(), session_id_generate() and other session related functions after outputting something on CLI - since headers are not sent on cli.

(The use case we have is unit-testing packages on cli)

Reproduce code:
echo 'a';

Expected result:

Actual result:
Warning: Cannot modify header information - headers already sent by (output started at /data/cvs/pear/sessionbug.php:2) in /data/cvs/pear/sessionbug.php on line 3

Call Stack:
    0.0003      83688   1. {main}() /data/cvs/pear/sessionbug.php:0
    0.0006      83988   2. header() /data/cvs/pear/sessionbug.php:3


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-04 14:03 UTC]
 [2013-06-27 22:41 UTC]
-Status: Open +Status: Not a bug -Package: Feature/Change Request +Package: *General Issues
 [2013-06-27 22:41 UTC]
Just use ob_start(). 

$ ./sapi/cli/php
echo 'a';

 [2013-07-03 12:50 UTC]
-Status: Not a bug +Status: Open
 [2013-07-03 12:50 UTC]
Using ob_start() is a workaround, not a fix. Also, error messages are hidden when using ob_start().
 [2014-05-02 12:54 UTC] mijahn dot ben at gmail dot com
Output buffering impedes the ability to view line by line errors when CLI unit testing.
 [2015-01-09 00:52 UTC]
Perhaps a better question is why header() is even available on CLI, or why you're using it in CLI.
 [2015-01-10 13:21 UTC]
@ajf: Not, those are not better questions.

If you had read the bug report description, you'd have noticed that I run unit tests which run code that uses header(). 14 plus votes do also stand against your "better question".
 [2015-01-12 19:57 UTC]
I'm confused: isn't it a _good_ thing that the CLI behaves the same way as other SAPIs in terms of header() handling for testing?
 [2015-01-12 20:27 UTC]
Header cannot be sent if body is sent already. It's the definition of HTTP. I fail to see what's wrong with this.

Besides, what the reason you need to call header() with CLI other than testing?
 [2015-01-12 20:30 UTC]
BTW, I cannot remove header() from CLI. CLI has built-in web server now.
 [2016-01-26 00:13 UTC] dsnaga78 at gmail dot com
There is no need to remove header from CLI. Why not adding a setting in php.ini to turn headers on/off? Something like:

cli_output.headers = On

Default is on, which is consistent with the existing behavior.

Such a setting will be used by CLI only and will definitely help in situations like those reported by the OP.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jun 13 01:01:31 2024 UTC