From fc15db40a94e54b7b7fedf8a2067ea774d379fa8 Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Fri, 27 Jun 2025 20:36:47 +0200 Subject: [PATCH 1/2] Warn on http_response_code() after header('HTTP/...') Fixes GH-18582 Fixes #81451 --- ext/standard/head.c | 5 +++++ sapi/cli/tests/gh18582.phpt | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 sapi/cli/tests/gh18582.phpt diff --git a/ext/standard/head.c b/ext/standard/head.c index ccef4be16bdfd..bf6cca1694c8b 100644 --- a/ext/standard/head.c +++ b/ext/standard/head.c @@ -375,6 +375,11 @@ PHP_FUNCTION(http_response_code) } RETURN_FALSE; } + + if (SG(sapi_headers).http_status_line) { + php_error_docref(NULL, E_WARNING, "Calling http_response_code() after header('HTTP/...') has no effect. This will change in PHP 9.0"); + } + zend_long old_response_code; old_response_code = SG(sapi_headers).http_response_code; diff --git a/sapi/cli/tests/gh18582.phpt b/sapi/cli/tests/gh18582.phpt new file mode 100644 index 0000000000000..d533689056d3e --- /dev/null +++ b/sapi/cli/tests/gh18582.phpt @@ -0,0 +1,40 @@ +--TEST-- +GH-18582: Allow http_response_code() to clear HTTP start-line +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +HTTP/1.1 404 Not Found +Host: %s +Date: %s +Connection: close +X-Powered-By: %s +Content-type: text/html; charset=UTF-8 + +
+Warning: http_response_code(): Calling http_response_code() after header('HTTP/...') has no effect. This will change in PHP 9.0 in %s on line 3
+404 +403 From 8a9bdcf0c55de1c3a6ec9b9dcdebd491c5ddb19e Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Tue, 22 Jul 2025 20:39:02 +0200 Subject: [PATCH 2/2] [skip ci] Keep code for PHP 9.0 in comment --- ext/standard/head.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ext/standard/head.c b/ext/standard/head.c index bf6cca1694c8b..a765902b448cd 100644 --- a/ext/standard/head.c +++ b/ext/standard/head.c @@ -378,6 +378,9 @@ 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. This will change in PHP 9.0"); + // TODO: Uncomment in PHP 9.0 + // efree(SG(sapi_headers).http_status_line); + // SG(sapi_headers).http_status_line = NULL; } zend_long old_response_code;