从 PHP 中的当前请求中获取 http 标头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13224615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:00:49  来源:igfitidea点击:

Get the http headers from current request in PHP

phpnginxhttp-headers

提问by Justin

Is it possible to get the http headers of the current request with PHP? I am notusing Apache as the web-server, but using nginx.

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

I tried using getallheaders()but I am getting Call to undefined function getallheaders().

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

回答by Layke

Taken from the documentation someone wrote a 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; 
    } 
} 

回答by JanDesch

Improved @Layke his function, making it a bit more secure to use it:

改进了@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;
    }
}

(wished I could just add this as a comment to his answer but still building on that reputation thingy -- one of my first replies)

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

回答by RafaSashi

Combined getallheaders() + apache_request_headers() for nginx

nginx 的组合 getallheaders() + apache_request_headers()

    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;
}

回答by Chris Wiegman

You can upgrade your server to PHP 5.4 thereby giving you access to getallheaders()via fastcgi or simply parse what you need out of $_SERVER with a foreachloop and a little regex.

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

回答by Geo Salameh

This issue was finally addressed in PHP 7.3.0, check release notes.

这个问题最终在 PHP 7.3.0 中得到解决,请查看发行说明

Fixed bug #62596(getallheaders() missing with PHP-FPM).

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

回答by Volomike

This should work:

这应该有效:

<?php 

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