[php-src] Issue #12303: Headers sent in CLI SAPI
| From: | joanhey | Date: | Tue, 26 Sep 2023 20:55:00 +0000 |
| Subject: | [php-src] Issue #12303: Headers sent in CLI SAPI | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245439@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/12303
Author: joanhey
### Description
It's an historical bug !!
The following code:
```php
<?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
<?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
<?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.
The php-src code in the CLI SAPI:
https://github.com/php/php-src/blob/master/sapi/cli/php_cli.c#L395-L400
**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