[英]PHP headers_list() is not showing all headers
According to the documentation: http://php.net/manual/en/function.headers-list.php , and this comment: http://php.net/manual/en/function.headers-list.php#110330 , php code: 根据文档: http : //php.net/manual/en/function.headers-list.php ,以及此评论: http : //php.net/manual/en/function.headers-list.php#110330 ,php代码:
<?php var_dump(header_list()); ?>
Does not show the status headers. 不显示状态标题。
This strange behavior is strange . 这种奇怪的行为很奇怪 。 So there are two questions: 所以有两个问题:
To post more code, simple workaround to header function (linear not object, using globals and not static class just to show the idea). 发布更多代码,简单的解决方法到头函数(线性非对象,使用全局而不是静态类只是为了显示想法)。 With the assumption that header function is omitting headers without colons (which may not be quite true...): 假设标题函数省略了没有冒号的标题(这可能不完全正确......):
<?php
// Mechanism:
$headers = array();
function setHeader($header) {
header($header);
if (strpos($header, ':') === false) {
global $headers;
$headers[] = $header;
}
}
function getHeaders() {
global $headers;
return array_merge($headers, header_list());
}
// Example:
setHeader('HTTP/1.1 404 Not Found');
var_dump(getHeaders());
?>
Checking the engine source for headers_list
and http_response_code
, notice that the value for general headers and status code are separated: 检查headers_list
和http_response_code
的引擎源 ,注意通用头和状态代码的值是分开的:
// headers_list
SG(sapi_headers).headers
// http_response_code
SG(sapi_headers).http_response_code
But HTTP response code isn't the only header with dedicated storage: Content-Type does, too : 但HTTP响应代码不是唯一具有专用存储的标头: Content-Type也是如此 :
SG(sapi_headers).mimetype = NULL;
So what's going on here? 那么这里发生了什么? The complete header() algorithm specifically checks for the following strings to adjust state: 完整的header()算法专门检查以下字符串以调整状态:
HTTP/
Content-Type
Content-Length
Location
WWW-Authenticate
HTTP/
is checked specifically because that's how one set the status code explicitly before PHP 5.4: after that, http_response_code
is available and is recommended for clarity. HTTP/
是专门检查的,因为这是在PHP 5.4之前显式设置状态代码的方式:之后, http_response_code
可用,为清楚起见,建议使用。 That header()
was used is confusing, for the reason you're asking in this question and on general principle: the http header BNF clearly doesn't include status line: 使用header()
是令人困惑的,因为你在这个问题和一般原则中提出的问题: http标题BNF显然不包括状态行:
header-field = field-name ":" OWS field-value OWS
PHP handles the others separately because they are single-value headers and/or their value matters for efficiency in later calculations. PHP分别处理其他因为它们是单值标题和/或它们的值对于以后计算中的效率很重要。
TL;DR: HTTP/
set by header()
isn't included in headers_list()
because HTTP/
status lines are not headers in the strict RFC sense. TL; DR: header()
HTTP/
设置的HTTP/
不包含在headers_list()
因为HTTP/
状态行不是严格RFC意义上的标题。 But for the PHP < 5.4 limitation that header()
was the only way to set HTTP/
status, it'd likely have never been a confusing issue. 但是对于PHP <5.4的限制, header()
是设置 HTTP/
状态的唯一方法 ,它可能从来就不是一个令人困惑的问题。
It seems that only the status code is missing from the header_list
. 似乎header_list
中只缺少状态代码。
You can get the current status code (they probably overwrite one another) using another function: http_response_code
. 您可以使用另一个函数获取当前状态代码(它们可能会相互覆盖): http_response_code
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.