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:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
12 + 50 = ?
Subscribe to this entry?

 
 [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>
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Nov 27 21:03:13 2021 UTC