Skip to content

http_response_code() does not override the status code generated by header() #18582

Open
@kkmuffme

Description

@kkmuffme

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:

  1. 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
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions