php.net |  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
Votes:24
Avg. Score:4.5 ± 0.8
Reproduced:24 of 24 (100.0%)
Same Version:2 (8.3%)
Same OS:16 (66.7%)
From: cweiske@php.net Assigned:
Status: Open Package: *General Issues
PHP Version: 5.2.4RC1 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: cweiske@php.net
New email:
PHP Version: OS:

 

 [2007-08-03 09:53 UTC] cweiske@php.net
Description:
------------
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:
---------------
<?php
echo 'a';
header('sadf');
?>


Expected result:
----------------
a

Actual result:
--------------
a
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


Patches

Pull Requests

History

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

$ ./sapi/cli/php
<?php
ob_start();
echo 'a';
header('sadf');
?>

a
 [2013-07-03 12:50 UTC] cweiske@php.net
-Status: Not a bug +Status: Open
 [2013-07-03 12:50 UTC] cweiske@php.net
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] ajf@php.net
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] cweiske@php.net
@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] aharvey@php.net
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] yohgaki@php.net
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] yohgaki@php.net
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 Dec 05 16:01:30 2024 UTC