php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81451 http_response_code() does not override the status code generated by header()
Submitted: 2021-09-17 03:12 UTC Modified: 2021-09-17 15:21 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: karpulix at ya dot ru Assigned:
Status: Verified Package: *Network Functions
PHP Version: 8.0.10 OS: Any
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2021-09-17 03:12 UTC] karpulix at ya dot ru
Description:
------------
http_response_code cannot override the status code generated by header
It doesn't work with php-server, nginx-server.

If it is not fixable, I guess it must be in the official documentation here https://www.php.net/manual/ru/function.http-response-code.php and the function must be recognized as deprecated. 

There is my question on Stackoverflow: https://stackoverflow.com/questions/69197771/why-is-function-http-response-code-acting-strange-that-was-called-after-functi


Test script:
---------------
<?php
header('HTTP/1.1 404 Not Found');
http_response_code(501);
http_response_code(502);
http_response_code(503);



Expected result:
----------------
HTTP/1.1 503 Service Unavailable
Host: localhost:9958
Date: Wed, 15 Sep 2021 17:20:05 GMT
Connection: close
X-Powered-By: PHP/8.0.10
Content-type: text/html; charset=UTF-8


Actual result:
--------------
HTTP/1.1 404 Not Found
Host: localhost:9958
Date: Wed, 15 Sep 2021 17:20:05 GMT
Connection: close
X-Powered-By: PHP/8.0.10
Content-type: text/html; charset=UTF-8


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-09-17 15:21 UTC] cmb@php.net
-Status: Open +Status: Verified -Package: FPM related +Package: *Network Functions
 [2021-09-17 15:21 UTC] cmb@php.net
Thank you for reporting this issue, and also for linking to the SO
question.  Pete's answer[1] is very useful, and spot on.

It seems to me the proper solution would be for http_response_code()
to call sapi_header_op(SAPI_HEADER_SET_STATUS, …) instead of manually
setting SG(sapi_headers).http_response_code.

[1] <https://stackoverflow.com/questions/69197771/why-is-function-http-response-code-acting-strange-that-was-called-after-functi#answer-69213593>
 [2022-06-10 09:27 UTC] afshin dot meh at gmail dot com
I'm interested to work on this issue if it's not already in progress.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 11:01:29 2024 UTC