Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Update fix for 18582 for http_response_code() to return correct HTTP …
…status

Updates the previous PR to fix 18582 to actually return the correct HTTP status code, which is sent to the browser
  • Loading branch information
kkmuffme committed Oct 2, 2025
commit 6ba07158af036de1257894ff628b270df3901ac0
5 changes: 3 additions & 2 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ PHP NEWS
. ReflectionConstant is no longer final. (sasezaki)

- SAPI:
. Fixed bug GH-18582 and #81451: http_response_code() does not override the
status code generated by header(). (ilutov, Jakub Zelenka)
. Fixed bug GH-18582 and #81451: http_response_code() now returns the correct
status code when it was set by header() and emits a warning if trying to
set a response code in that case. (ilutov, Jakub Zelenka, kkmuffme)

- Standard:
. Passing strings which are not one byte long to ord() is now deprecated.
Expand Down
14 changes: 13 additions & 1 deletion ext/standard/head.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ PHP_FUNCTION(headers_list)
PHP_FUNCTION(http_response_code)
{
zend_long response_code = 0;
char *s;

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Expand All @@ -384,7 +385,12 @@ PHP_FUNCTION(http_response_code)

if (SG(sapi_headers).http_status_line) {
php_error_docref(NULL, E_WARNING, "Calling http_response_code() after header('HTTP/...') has no effect");
// If it is decided that this should have effect in the future, replace warning with
if ((s = strchr(SG(sapi_headers).http_status_line, ' '))) {
SG(sapi_headers).http_response_code = atoi((s + 1));
RETURN_LONG(SG(sapi_headers).http_response_code);
}
// If it is decided that this should have effect in the future,
// remove the warning and RETURN_LONG and uncomment below
// efree(SG(sapi_headers).http_status_line);
// SG(sapi_headers).http_status_line = NULL;
}
Expand All @@ -401,6 +407,12 @@ PHP_FUNCTION(http_response_code)
RETURN_TRUE;
}

if (SG(sapi_headers).http_status_line &&
(s = strchr(SG(sapi_headers).http_status_line, ' '))
) {
SG(sapi_headers).http_response_code = atoi((s + 1));
}

if (!SG(sapi_headers).http_response_code) {
RETURN_FALSE;
}
Expand Down
6 changes: 3 additions & 3 deletions sapi/cli/tests/gh18582.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ php_cli_server_start(<<<'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();
$should_be_404_not_403 = http_response_code();
echo $is_404 . PHP_EOL;
echo $should_be_404_but_is_403 . PHP_EOL;
echo $should_be_404_not_403 . PHP_EOL;
PHP);

$host = PHP_CLI_SERVER_HOSTNAME;
Expand All @@ -37,4 +37,4 @@ Content-type: text/html; charset=UTF-8
<br />
<b>Warning</b>: http_response_code(): Calling http_response_code() after header('HTTP/...') has no effect in <b>%s</b> on line <b>3</b><br />
404
403
404
Loading