2024 年 PHP 日本会议

http_response_code

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

http_response_code获取或设置 HTTP 响应代码

描述

http_response_code(int $response_code = 0): int|bool

获取或设置 HTTP 响应状态代码。

参数

response_code

可选的 response_code 将设置响应代码。

返回值

如果提供了 response_code,则返回之前的状态代码。如果没有提供 response_code,则返回当前状态代码。如果在 Web 服务器环境中使用,这两个值都默认为 200 状态代码。

如果未提供 response_code 且不在 Web 服务器环境(例如 CLI 应用程序)中调用,则返回 false。如果提供了 response_code 且不在 Web 服务器环境中调用(但仅当未设置先前的响应状态时),则返回 true

示例

示例 #1 在 Web 服务器环境中使用 http_response_code()

<?php

// 获取当前响应代码并设置新的响应代码
var_dump(http_response_code(404));

// 获取新的响应代码
var_dump(http_response_code());
?>

以上示例将输出

int(200)
int(404)

示例 #2 在 CLI 环境中使用 http_response_code()

<?php

// 获取当前默认响应代码
var_dump(http_response_code());

// 设置响应代码
var_dump(http_response_code(201));

// 获取新的响应代码
var_dump(http_response_code());
?>

以上示例将输出

bool(false)
bool(true)
int(201)

参见

添加注释

用户贡献的注释 12 条注释

craig at craigfrancis dot co dot uk
12 年前
如果您的 PHP 版本不包含此函数

<?php

if (!function_exists('http_response_code')) {
function
http_response_code($code = NULL) {

if (
$code !== NULL) {

switch (
$code) {
case
100: $text = 'Continue'; break;
case
101: $text = 'Switching Protocols'; break;
case
200: $text = 'OK'; break;
case
201: $text = 'Created'; break;
case
202: $text = 'Accepted'; break;
case
203: $text = 'Non-Authoritative Information'; break;
case
204: $text = 'No Content'; break;
case
205: $text = 'Reset Content'; break;
case
206: $text = 'Partial Content'; break;
case
300: $text = 'Multiple Choices'; break;
case
301: $text = 'Moved Permanently'; break;
case
302: $text = 'Moved Temporarily'; break;
case
303: $text = 'See Other'; break;
case
304: $text = 'Not Modified'; break;
case
305: $text = 'Use Proxy'; break;
case
400: $text = 'Bad Request'; break;
case
401: $text = 'Unauthorized'; break;
case
402: $text = 'Payment Required'; break;
case
403: $text = 'Forbidden'; break;
case
404: $text = 'Not Found'; break;
case
405: $text = 'Method Not Allowed'; break;
case
406: $text = 'Not Acceptable'; break;
case
407: $text = 'Proxy Authentication Required'; break;
case
408: $text = 'Request Time-out'; break;
case
409: $text = 'Conflict'; break;
case
410: $text = 'Gone'; break;
case
411: $text = 'Length Required'; break;
case
412: $text = 'Precondition Failed'; break;
case
413: $text = 'Request Entity Too Large'; break;
case
414: $text = 'Request-URI Too Large'; break;
case
415: $text = 'Unsupported Media Type'; break;
case
500: $text = 'Internal Server Error'; break;
case
501: $text = 'Not Implemented'; break;
case
502: $text = 'Bad Gateway'; break;
case
503: $text = 'Service Unavailable'; break;
case
504: $text = 'Gateway Time-out'; break;
case
505: $text = 'HTTP Version not supported'; break;
default:
exit(
'Unknown http status code "' . htmlentities($code) . '"');
break;
}

$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');

header($protocol . ' ' . $code . ' ' . $text);

$GLOBALS['http_response_code'] = $code;

} else {

$code = (isset($GLOBALS['http_response_code']) ? $GLOBALS['http_response_code'] : 200);

}

return
$code;

}
}

?>

本例中使用了 `$GLOBALS`,但您可以使用任何您喜欢的存储机制……我认为无法返回当前状态码。

https://bugs.php.net/bug.php?id=52555

参考我从PHP源码中获得的错误代码

http://lxr.php.net/opengrok/xref/PHP_5_4/sapi/cgi/cgi_main.c#354

以及当前HTTP头是如何发送的,以及它使用的变量。

http://lxr.php.net/opengrok/xref/PHP_5_4/main/SAPI.c#856
Stefan W
10年前
请注意,您不能使用此函数设置任意的响应代码,只能设置PHP(或PHP运行的SAPI)已知的那些代码。

以下代码目前按预期工作(PHP作为Apache模块运行)
200 – 208, 226
300 – 305, 307, 308
400 – 417, 422 – 424, 426, 428 – 429, 431
500 – 508, 510 – 511

代码0、100、101和102将被发送为“200 OK”。

其他所有代码都将导致“500 Internal Server Error”。

如果您想发送具有自由格式状态行的响应,则需要使用`header()`函数。

<?php header("HTTP/1.0 418 I'm A Teapot"); ?>
Thomas A. P.
9年前
当将响应代码设置为非标准代码(如420)时,Apache输出500 Internal Server Error。

当使用header(0,0,420)和http_response_code(420)时,会发生这种情况。
改用header('HTTP/1.1 420 Enhance Your Calm')。

请注意,字符串中的响应代码会被解释并用于访问日志和通过http_response_code()输出。
divinity76 at gmail dot com
4年前
如果您需要http_response_code()不支持的响应代码,例如WebDAV/RFC4918的“HTTP 507 Insufficient Storage”,请尝试

<?php
header
($_SERVER['SERVER_PROTOCOL'] . ' 507 Insufficient Storage');
?>
结果:类似于

HTTP/1.1 507 Insufficient Storage
匿名用户
11年前
状态码作为数组

<?php
$http_status_codes
= array(100 => "继续", 101 => "切换协议", 102 => "处理中", 200 => "成功", 201 => "已创建", 202 => "已接受", 203 => "非权威信息", 204 => "无内容", 205 => "重置内容", 206 => "部分内容", 207 => "多状态", 300 => "多种选择", 301 => "永久移动", 302 => "临时移动", 303 => "查看其他", 304 => "未修改", 305 => "使用代理", 306 => "(未使用)", 307 => "临时重定向", 308 => "永久重定向", 400 => "错误请求", 401 => "未授权", 402 => "需要付费", 403 => "禁止", 404 => "未找到", 405 => "方法不允许", 406 => "不可接受", 407 => "需要代理身份验证", 408 => "请求超时", 409 => "冲突", 410 => "已删除", 411 => "需要长度", 412 => "前提条件失败", 413 => "请求实体过大", 414 => "请求URI过长", 415 => "不支持的媒体类型", 416 => "请求范围不满足", 417 => "期望失败", 418 => "我是茶壶", 419 => "身份验证超时", 420 => "保持冷静", 422 => "不可处理的实体", 423 => "锁定", 424 => "失败的依赖", 424 => "方法失败", 425 => "无序集合", 426 => "需要升级", 428 => "需要前提条件", 429 => "请求过多", 431 => "请求头字段过大", 444 => "无响应", 449 => "重试", 450 => "被Windows家长控制阻止", 451 => "因法律原因不可用", 494 => "请求头过大", 495 => "证书错误", 496 => "无证书", 497 => "HTTP转HTTPS", 499 => "客户端关闭请求", 500 => "内部服务器错误", 501 => "未实现", 502 => "错误网关", 503 => "服务不可用", 504 => "网关超时", 505 => "HTTP版本不支持", 506 => "变体也在协商", 507 => "存储空间不足", 508 => "检测到循环", 509 => "带宽限制超过", 510 => "未扩展", 511 => "需要网络身份验证", 598 => "网络读取超时错误", 599 => "网络连接超时错误");
?>

来源:维基百科“HTTP状态码列表”
[email protected]
4年前
不要混合使用 `http_response_code()` 和手动设置响应代码头,因为最终由 Web 服务器返回的实际 HTTP 状态代码可能与预期不符。如果响应代码之前已使用 `header()` 函数设置,则 `http_response_code()` 将不起作用。示例

<?php
header
('HTTP/1.1 401 Unauthorized');
http_response_code(403);
print(
http_response_code());
?>

原始 HTTP 响应将为(注意第一行中的实际状态代码与正文中打印的 `http_response_code` 不匹配)

HTTP/1.1 401 Unauthorized
Date: Tue, 24 Nov 2020 13:49:08 GMT
Server: Apache
Connection: Upgrade, Keep-Alive
Keep-Alive: timeout=5, max=100
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

403

我只在 Apache 上测试过它。我不确定此行为是 Apache 特有的,还是所有 PHP 发行版都通用的。
Rob Zazueta
11年前
上面来自“匿名”的注释是错误的。我在 AWS 弹性负载均衡器后面运行它,并且尝试上面提到的 `header(':'.$error_code...)` 方法被视为无效的 HTTP。

如果您仍然使用的是 < php 5.4,则 `header()` 函数的文档说明了实现此方法的正确方法。

<?php
header
("HTTP/1.0 404 Not Found");
?>
匿名
12 年前
如果您没有 PHP 5.4 并想更改返回的状态代码,您可以简单地编写
<?php
header
(':', true, $statusCode);
?>

冒号 ':' 是必需的,否则不起作用。
Steven
9年前
`http_response_code` 基本上是编写 HTTP 状态头的简写方式,额外的好处是 PHP 将通过将其响应代码与它在 php-src/main/http_status_codes.h 中维护的枚举中的一个值匹配来计算出合适的理由短语。请注意,这意味着您的响应代码必须与 PHP 已知的响应代码匹配。您不能使用此方法创建自己的响应代码,但是您可以使用 `header` 方法。

总而言之 - 设置响应代码时,“`http_response_code`”和“`header`”之间的区别

1. 使用 `http_response_code` 将导致 PHP 从硬编码到 PHP 源代码中的理由短语列表中匹配并应用理由短语。

2. 由于上述第 1 点,如果您使用 `http_response_code`,则必须设置 PHP 已知的代码。您不能设置自己的自定义代码,但是如果您使用 `header` 方法,则可以设置自定义代码(和理由短语)。
Richard F.
11年前
至少在我使用 php-fpm 和 nginx 的情况下,此方法不会更改响应中的文本,只会更改代码。

<?php

// HTTP/1.1 404 Not Found
http_response_code(404);

?>

生成的响应为 HTTP/1.1 404 OK
[email protected]
10年前
没有明确提及,但在设置时,返回值是旧的状态码。
例如:
<?php

$a
= http_response_code();
$b = http_response_code(202);
$c = http_response_code();

var_dump($a, $b, $c);

// 结果:
// int(200)
// int(200)
// int(202)
?>
[email protected]
9年前
@[email protected] 写了一个替换原始函数的函数。它非常有用,但有一个错误。原始的 `http_response_code` 总是返回之前的或当前的代码,而不是您现在正在设置的代码。这是我修正后的版本。我还使用 `$GLOBALS` 来存储当前代码,但使用 `trigger_error()` 而不是 `exit()`。所以现在,函数在出错时的行为取决于错误处理程序。或者您可以将其改回 `exit()`。

如果 (!function_exists('http_response_code')) {
function http_response_code($code = NULL) {
$prev_code = (isset($GLOBALS['http_response_code']) ? $GLOBALS['http_response_code'] : 200);

if ($code === NULL) {
return $prev_code;
}

switch ($code) {
case 100: $text = '继续'; break;
case 101: $text = '切换协议'; break;
case 200: $text = '成功'; break;
case 201: $text = '已创建'; break;
case 202: $text = '已接受'; break;
case 203: $text = '非权威信息'; break;
case 204: $text = '无内容'; break;
case 205: $text = '重置内容'; break;
case 206: $text = '部分内容'; break;
case 300: $text = '多种选择'; break;
case 301: $text = '永久移动'; break;
case 302: $text = '临时移动'; break;
case 303: $text = '查看其他'; break;
case 304: $text = '未修改'; break;
case 305: $text = '使用代理'; break;
case 400: $text = '错误请求'; break;
case 401: $text = '未授权'; break;
case 402: $text = '需要付费'; break;
case 403: $text = '禁止'; break;
case 404: $text = '未找到'; break;
case 405: $text = '方法不允许'; break;
case 406: $text = '不可接受'; break;
case 407: $text = '需要代理身份验证'; break;
case 408: $text = '请求超时'; break;
case 409: $text = '冲突'; break;
case 410: $text = '已消失'; break;
case 411: $text = '需要长度'; break;
case 412: $text = '前提条件失败'; break;
case 413: $text = '请求实体过大'; break;
case 414: $text = '请求URI过长'; break;
case 415: $text = '不支持的媒体类型'; break;
case 500: $text = '内部服务器错误'; break;
case 501: $text = '未实现'; break;
case 502: $text = '错误网关'; break;
case 503: $text = '服务不可用'; break;
case 504: $text = '网关超时'; break;
case 505: $text = 'HTTP版本不支持'; break;
default
trigger_error('未知的HTTP状态码 ' . $code, E_USER_ERROR); // exit('Unknown http status code "' . htmlentities($code) . '"');
return $prev_code;
}

$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
header($protocol . ' ' . $code . ' ' . $text);
$GLOBALS['http_response_code'] = $code;

// 原函数始终返回之前的或当前代码
return $prev_code;
}
}
To Top