Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f389ade

Browse files
iluuu1994bukka
andcommitted
Warn on http_response_code() after header('HTTP/...')
Fixes GH-18582 Fixes #81451 Closes GH-18962 Co-authored-by: Jakub Zelenka <bukka@php.net>
1 parent e23c622 commit f389ade

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

‎NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ PHP NEWS
5656
- Reflection:
5757
. ReflectionConstant is no longer final. (sasezaki)
5858

59+
- SAPI:
60+
. Fixed bug GH-18582 and #81451: http_response_code() does not override the
61+
status code generated by header(). (ilutov, Jakub Zelenka)
62+
5963
- Standard:
6064
. Passing strings which are not one byte long to ord() is now deprecated.
6165
(Girgias)

‎ext/standard/head.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,14 @@ PHP_FUNCTION(http_response_code)
381381
}
382382
RETURN_FALSE;
383383
}
384+
385+
if (SG(sapi_headers).http_status_line) {
386+
php_error_docref(NULL, E_WARNING, "Calling http_response_code() after header('HTTP/...') has no effect");
387+
// If it is decided that this should have effect in the future, replace warning with
388+
// efree(SG(sapi_headers).http_status_line);
389+
// SG(sapi_headers).http_status_line = NULL;
390+
}
391+
384392
zend_long old_response_code;
385393

386394
old_response_code = SG(sapi_headers).http_response_code;

‎sapi/cli/tests/gh18582.phpt‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
GH-18582: Allow http_response_code() to clear HTTP start-line
3+
--SKIPIF--
4+
<?php
5+
include "skipif.inc";
6+
?>
7+
--FILE--
8+
<?php
9+
include "php_cli_server.inc";
10+
11+
php_cli_server_start(<<<'PHP'
12+
http_response_code(401);
13+
header('HTTP/1.1 404 Not Found');
14+
$is_404 = http_response_code(403);
15+
$should_be_404_but_is_403 = http_response_code();
16+
echo $is_404 . PHP_EOL;
17+
echo $should_be_404_but_is_403 . PHP_EOL;
18+
PHP);
19+
20+
$host = PHP_CLI_SERVER_HOSTNAME;
21+
$fp = php_cli_server_connect();
22+
if (fwrite($fp, "GET / HTTP/1.1\nHost: {$host}\n\n")) {
23+
while (!feof($fp)) {
24+
echo fgets($fp);
25+
}
26+
}
27+
fclose($fp);
28+
?>
29+
--EXPECTF--
30+
HTTP/1.1 404 Not Found
31+
Host: %s
32+
Date: %s
33+
Connection: close
34+
X-Powered-By: %s
35+
Content-type: text/html; charset=UTF-8
36+
37+
<br />
38+
<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 />
39+
404
40+
403

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /