Description
Description
It's an historical bug !!
The following code:
<?php
echo 'Hello';
ini_set('session.name', 'SID');
Resulted in this output:
Hello
Warning: ini_set(): Session ini settings cannot be changed after headers have already been sent in /in/HIReN on line 4
But I expected this output instead:
Hello
For a lot of time the developers by pass this problem with tricks:
https://3v4l.org/rJdZo
<?php
fwrite(STDOUT, 'Hello');
ini_set('session.name', 'SID');
fwrite(STDOUT, "\n" . ini_get('session.name'));
And also setting an error handler, because when output an error happen the same:
https://3v4l.org/3lEcj
<?php
set_error_handler(function ($code, $msg, $file, $line) {
fwrite(\STDERR, "$msg in file $file on line $line\n");
});
$foo = $bar;
ini_set('session.name', 'SID');
echo ini_get('session.name'), PHP_EOL;
The first difference of the CLI SAPI is that no headers are written to the output !!
https://www.php.net/manual/en/features.commandline.differences.php
Still show the error: headers have already been sent in ...
, when the default behavior is to send the output to terminal, without the need for extra tricks. Using fwrite(), error_handler() or an output buffer.
The php-src code in the CLI SAPI:
https://github.com/php/php-src/blob/master/sapi/cli/php_cli.c#L390-L393
BC: Nothing
In fact we can simplify cli apps by removing the tricks to by pass this problem.
PHP Version
All versions
Operating System
All