1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

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
This commit is contained in:
Calvin Buckley
2023-03-01 13:30:15 -04:00
committed by Ilija Tovilo
parent c02348cf9d
commit 3af5f47ce6
4 changed files with 26 additions and 3 deletions

2
NEWS
View File

@@ -184,6 +184,8 @@ PHP NEWS
. Fix GH-11010 (parse_ini_string() now preserves formatting of unquoted
strings starting with numbers when the INI_SCANNER_TYPED flag is
specified). (ilutov)
. Fix GH-10742 (http_response_code emits no error when headers were already
sent). (NattyNarwhal)
- Streams:
. Fixed bug #51056: blocking fread() will block even if data is available.

View File

@@ -363,6 +363,18 @@ PHP_FUNCTION(http_response_code)
if (response_code)
{
if (SG(headers_sent) && !SG(request_info).no_headers) {
const char *output_start_filename = php_output_get_start_filename();
int output_start_lineno = php_output_get_start_lineno();
if (output_start_filename) {
php_error_docref(NULL, E_WARNING, "Cannot set response code - headers already sent "
"(output started at %s:%d)", output_start_filename, output_start_lineno);
} else {
php_error_docref(NULL, E_WARNING, "Cannot set response code - headers already sent");
}
RETURN_FALSE;
}
zend_long old_response_code;
old_response_code = SG(sapi_headers).http_response_code;

View File

@@ -21,8 +21,17 @@ var_dump(
// Get the new response code
http_response_code()
);
echo "Now we've sent the headers\n";
var_dump(
// This should fail
http_response_code(500)
);
?>
--EXPECT--
--EXPECTF--
bool(false)
bool(true)
int(201)
Now we've sent the headers
Warning: http_response_code(): Cannot set response code - headers already sent (output started at %s:%d) in %s on line %d
bool(false)

View File

@@ -38,7 +38,7 @@ function doTestCalls(FPM\Tester &$tester, bool $expectSuppressableEntries)
$tester->request(query: 'test=output', uri: '/ping')->expectBody('pong', 'text/plain');
$tester->expectAccessLog("'GET /ping?test=output' 200", suppressable: false);
$tester->request(headers: ['X_ERROR' => 1])->expectBody('Not OK');
$tester->request(headers: ['X_ERROR' => 1])->expectStatus('500 Internal Server Error')->expectBody('Not OK');
$tester->expectAccessLog("'GET /log-suppress-output.src.php' 500", suppressable: false);
$tester->request()->expectBody('OK');
@@ -54,8 +54,8 @@ function doTestCalls(FPM\Tester &$tester, bool $expectSuppressableEntries)
$src = <<<EOT
<?php
if (isset(\$_SERVER['X_ERROR'])) {
echo "Not OK";
http_response_code(500);
echo "Not OK";
exit;
}
echo \$_REQUEST['test'] ?? "OK";