【问题标题】:Get the http headers from current request in PHP从 PHP 中的当前请求中获取 http 标头
【发布时间】:2012-10-24 20:19:04
【问题描述】:

是否可以使用 PHP 获取当前请求的 http 标头?我使用 Apache 作为 Web 服务器,而是使用 nginx。

我尝试使用getallheaders(),但我得到了Call to undefined function getallheaders()

【问题讨论】:

标签: php nginx http-headers


【解决方案1】:

您可以将服务器升级到 PHP 5.4,从而让您可以通过 fastcgi 访问 getallheaders(),或者只需使用 foreach 循环和一些正则表达式从 $_SERVER 解析您需要的内容。

【讨论】:

  • nginx 是否总是在 FastCGI 上运行?这就是getallheaders() 在 PHP 5.3 下不起作用的原因吗?
  • @BenHarold 查看getallheaders 的更改日志:5.4:此功能在 FastCGI 下可用。以前,仅当 PHP 作为 Apache 模块安装时才受支持。
  • @FredWuerges 我确实阅读了更改日志。这就是我问这些问题的原因。说得好一点:nginx总是使用FastCGI,这就是为什么getallheaders()在使用PHP 5.3或更早版本的nginx时不起作用的原因?这是否意味着 getallheaders()apache_request_headers() 在使用 PHP 5.4 和 nginx 时都可以工作?
  • 这在 nginx 上仍然不起作用,getallheaders 被归档在 PHP 文档中的 apache 函数下,因为它只是 Apache,正如在 php 5.5 和 nginx 上确认的那样
  • 目前在 PHP7 上 getallheaders 在 FastCGI 下的 nginx 上不起作用
【解决方案2】:

取自文档中有人写了一个comment...

if (!function_exists('getallheaders')) 
{ 
    function getallheaders() 
    { 
       $headers = array (); 
       foreach ($_SERVER as $name => $value) 
       { 
           if (substr($name, 0, 5) == 'HTTP_') 
           { 
               $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; 
           } 
       } 
       return $headers; 
    } 
} 

【讨论】:

  • 谢谢它的工作。但是您能解释一下ucwordsstrtolower 在该函数中的用途吗?有必要吗?
  • 此函数中的一个错误是“DNT”(Do Not Track)等大写标题将变为“Dnt” - 这不是本机 getallheaders() 的情况
  • 这个功能没有出现“授权”...有什么想法吗?
【解决方案3】:

改进了@Layke 的功能,使其使用起来更加安全:

if (!function_exists('getallheaders'))  {
    function getallheaders()
    {
        if (!is_array($_SERVER)) {
            return array();
        }

        $headers = array();
        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }
}

(希望我可以将其作为评论添加到他的回答中,但仍然建立在这种声誉之上——我的第一个回复之一)

【讨论】:

    【解决方案4】:

    组合 getallheaders() + apache_request_headers() 用于 nginx

        function get_nginx_headers($function_name='getallheaders'){
    
            $all_headers=array();
    
            if(function_exists($function_name)){ 
    
                $all_headers=$function_name();
            }
            else{
    
                foreach($_SERVER as $name => $value){
    
                    if(substr($name,0,5)=='HTTP_'){
    
                        $name=substr($name,5);
                        $name=str_replace('_',' ',$name);
                        $name=strtolower($name);
                        $name=ucwords($name);
                        $name=str_replace(' ', '-', $name);
    
                        $all_headers[$name] = $value; 
                    }
                    elseif($function_name=='apache_request_headers'){
    
                        $all_headers[$name] = $value; 
                    }
                }
            }
    
    
            return $all_headers;
    }
    

    【讨论】:

      【解决方案5】:

      这应该可行:

      <?php 
      
      print_r(
        array_intersect_key(
          $_SERVER,
          array_flip(
            preg_grep(
              '/^HTTP_/', 
              array_keys($_SERVER),
              0
            )
          )
        )
      );
      

      【讨论】:

        【解决方案6】:

        这个问题终于在 PHP 7.3.0 中得到解决,检查release notes

        修复了错误 #62596(PHP-FPM 缺少 getallheaders())。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-18
          • 2015-10-18
          • 2020-02-13
          相关资源
          最近更新 更多