简体   繁体   English

PHP headers_list()未显示所有标头

[英]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: 所以有两个问题:

  1. Why? 为什么? (I'm not sure if this question is opinion based, if it is, and there is no REAL explanation please omit it. I mean that sometimes opinion based questions aren't opinion based, and really have explanation, and this cannot be predict before they are asked). (我不确定这个问题是否基于意见,如果是,并且没有真正的解释请省略它。我的意思是有时候基于意见的问题不是基于意见的,而且确实有解释,这无法预测在他们被问到之前)。
  2. I know that I can use my own function to set header, which will set header and additionally remember that this header was set. 我知道我可以使用自己的函数来设置标题,这将设置标题并另外记住此标题已设置。 But this is kind of... workaround, as header_list() is quite sure HERE, FOR THAT . 但这有点......解决方法,因为header_list()在这里非常肯定,因为那样。 Additionally those headers are somewhere in the php engine memory so saving them second time inside script is not memory efficient. 另外这些头文件位于php引擎内存中,因此在脚本内第二次保存它们并不是内存效率高。 So... What is the back-door to get all headers, not as stupid as workaround below? 那么...... 获取所有标题的后门是什么,而不是像下面的解决方法那样愚蠢? This can be useful for example as a part of debug / developer class that is rendering all the "developer" data as html comments at the end of the page. 这可能是有用的,例如作为调试/开发人员类的一部分,它将所有“开发人员”数据呈现为页面末尾的html注释。 Of course I'm omitting the content length header which is too soon to predict. 当然我省略了内容长度标题,这个标题太早了。
  3. It looks like this function omit all the headers that don't have colon... Is it right? 看起来这个函数省略了没有冒号的所有标题......是不是?

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_listhttp_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM