Open
Description
Description
Originally reported here https://bugs.php.net/bug.php?id=81451
However, while the original issue won't necessarily require a fix/just a documentation update, I want to take this issue further and report it as a bug, since this behavior makes it impossible to retrieve the actual HTTP status code after it was changed with http_response_status()
The following code:
<?php
http_response_code( 401 );
header( 'HTTP/1.1 404 Not Found' );
$is_404 = http_response_code( 403 );
$should_be_404_but_is_403 = http_response_code();
echo $is_404 . PHP_EOL;
echo $should_be_404_but_is_403 . PHP_EOL;
Resulted in this output:
404
403
But I expected this output instead:
404
404
Since headers_list() does not contain the original header() either, it's impossible to identify the actual HTTP response code when the HTTP response header was set manually and it was later overwritten (since the overwritten value is not actually used)
I think the way forward with this issue is:
- if the header is manually set already and http_response_code( 123 ) is used, PHP should emit a warning, that this has no effect since the HTTP status is manually set
- with the next major version, remove this warning again and actually fix the behavior, so http_response_code will actually overwrite any manual HTTP response code headers
PHP Version
PHP 8.4
Operating System
No response