PHP headers_list()未显示所有标题 [英] PHP headers_list() is not showing all headers

查看:84
本文介绍了PHP headers_list()未显示所有标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档: http://php.net/manual/zh-CN /function.headers-list.php ,然后输入以下注释: http://php.net/manual/zh-CN/function.headers-list.php#110330 ,php代码:

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:

<?php var_dump(header_list()); ?>

不显示状态标题.

这种奇怪的行为很奇怪.所以有两个问题:

  1. 为什么?(我不确定这个问题是否基于观点,如果是,并且没有REAL解释,请忽略它.我的意思是有时基于观点的问题不是观点的基础上,并且确实有解释,并且在被要求之前无法预测).
  2. 我知道我可以使用自己的函数来设置标头,该标头将设置标头,另外还要记住已设置此标头.但这是一种...解决方法,因为header_list()可以确定在这里做什么.此外,这些标头位于php引擎内存中的某个位置,因此第二次将其保存在脚本中对内存效率不高.所以... 获取所有标头的后门是什么,而不是下面的变通方法那么愚蠢?例如,这在作为调试/开发人员类的一部分来呈现所有开发人员"时会很有用.数据以html注释的形式显示在页面末尾. 当然,我省略了内容长度标头,现在还无法预测.
  3. 该函数似乎忽略了所有没有冒号的标题...对吗?
  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. Additionally those headers are somewhere in the php engine memory so saving them second time inside script is not memory efficient. 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. 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_list
SG(sapi_headers).headers

// http_response_code
SG(sapi_headers).http_response_code

但是HTTP响应代码不是唯一具有专用存储空间的标头:

But HTTP response code isn't the only header with dedicated storage: Content-Type does, too:

SG(sapi_headers).mimetype = NULL;

那么这是怎么回事? 完整标头()算法专门检查以下字符串以调整状态:

So what's going on here? The complete header() algorithm specifically checks for the following strings to adjust state:

  • HTTP/
  • Content-Type
  • Content-Length
  • Location
  • WWW-Authenticate
  • HTTP/
  • Content-Type
  • Content-Length
  • Location
  • WWW-Authenticate

HTTP/是因为这是在PHP 5.4之前显式设置状态代码的方式:此后,

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. 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-field   = field-name ":" OWS field-value OWS

PHP分别处理其他对象,因为它们是单值标头和/或它们的值对于以后的计算效率很重要.

PHP handles the others separately because they are single-value headers and/or their value matters for efficiency in later calculations.

TL; DR:headers_list()中不包括由header()设置的HTTP/,因为严格意义上的RFC来说,HTTP/状态行不是标头.但是对于PHP< 5.4限制header()设置状态的唯一方法 HTTP/,它可能从来就不是一个令人困惑的问题.

TL;DR: HTTP/ set by header() isn't included in headers_list() because HTTP/ status lines are not headers in the strict RFC sense. 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 headers_list()未显示所有标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆