Skip to content

Commit 3af5f47

Browse files
NattyNarwhaliluuu1994
authored andcommitted
http_response_code should warn if headers were already sent
This would previously fail silently. We also return false to indicate the error. Fixes GH-10742 Closes GH-10744
1 parent c02348c commit 3af5f47

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ PHP NEWS
184184
. Fix GH-11010 (parse_ini_string() now preserves formatting of unquoted
185185
strings starting with numbers when the INI_SCANNER_TYPED flag is
186186
specified). (ilutov)
187+
. Fix GH-10742 (http_response_code emits no error when headers were already
188+
sent). (NattyNarwhal)
187189

188190
- Streams:
189191
. Fixed bug #51056: blocking fread() will block even if data is available.

ext/standard/head.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,18 @@ PHP_FUNCTION(http_response_code)
363363

364364
if (response_code)
365365
{
366+
if (SG(headers_sent) && !SG(request_info).no_headers) {
367+
const char *output_start_filename = php_output_get_start_filename();
368+
int output_start_lineno = php_output_get_start_lineno();
369+
370+
if (output_start_filename) {
371+
php_error_docref(NULL, E_WARNING, "Cannot set response code - headers already sent "
372+
"(output started at %s:%d)", output_start_filename, output_start_lineno);
373+
} else {
374+
php_error_docref(NULL, E_WARNING, "Cannot set response code - headers already sent");
375+
}
376+
RETURN_FALSE;
377+
}
366378
zend_long old_response_code;
367379

368380
old_response_code = SG(sapi_headers).http_response_code;

ext/standard/tests/general_functions/http_response_code.phpt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ var_dump(
2121
// Get the new response code
2222
http_response_code()
2323
);
24+
echo "Now we've sent the headers\n";
25+
var_dump(
26+
// This should fail
27+
http_response_code(500)
28+
);
2429
?>
25-
--EXPECT--
30+
--EXPECTF--
2631
bool(false)
2732
bool(true)
2833
int(201)
34+
Now we've sent the headers
35+
36+
Warning: http_response_code(): Cannot set response code - headers already sent (output started at %s:%d) in %s on line %d
37+
bool(false)

sapi/fpm/tests/log-suppress-output.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function doTestCalls(FPM\Tester &$tester, bool $expectSuppressableEntries)
3838
$tester->request(query: 'test=output', uri: '/ping')->expectBody('pong', 'text/plain');
3939
$tester->expectAccessLog("'GET /ping?test=output' 200", suppressable: false);
4040

41-
$tester->request(headers: ['X_ERROR' => 1])->expectBody('Not OK');
41+
$tester->request(headers: ['X_ERROR' => 1])->expectStatus('500 Internal Server Error')->expectBody('Not OK');
4242
$tester->expectAccessLog("'GET /log-suppress-output.src.php' 500", suppressable: false);
4343

4444
$tester->request()->expectBody('OK');
@@ -54,8 +54,8 @@ function doTestCalls(FPM\Tester &$tester, bool $expectSuppressableEntries)
5454
$src = <<<EOT
5555
<?php
5656
if (isset(\$_SERVER['X_ERROR'])) {
57-
echo "Not OK";
5857
http_response_code(500);
58+
echo "Not OK";
5959
exit;
6060
}
6161
echo \$_REQUEST['test'] ?? "OK";

0 commit comments

Comments
 (0)