Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kkmuffme opened this issue May 17, 2025 · 0 comments
Open

Comments

@kkmuffme
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant