diff --git a/NEWS b/NEWS
index ece950165ad..fbf733889a8 100644
--- a/NEWS
+++ b/NEWS
@@ -56,6 +56,10 @@ PHP NEWS
- Reflection:
. 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)
+
- Standard:
. Passing strings which are not one byte long to ord() is now deprecated.
(Girgias)
diff --git a/ext/standard/head.c b/ext/standard/head.c
index f94c7f596c2..087ba6a3480 100644
--- a/ext/standard/head.c
+++ b/ext/standard/head.c
@@ -381,6 +381,14 @@ 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");
+ // If it is decided that this should have effect in the future, replace warning with
+ // efree(SG(sapi_headers).http_status_line);
+ // SG(sapi_headers).http_status_line = NULL;
+ }
+
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 00000000000..0c3cebf71c3
--- /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 in %s on line 3
+404
+403