如何让 PHP 显示它从浏览器收到的标头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1403670/
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
How can I get PHP to display the headers it received from a browser?
提问by tehryan
Are they all stored in $_SERVER? Even custom ones?
它们都存储在$_SERVER? 甚至定制的?
回答by Steven Mercatante
Try this
尝试这个
print_r($_SERVER)
print_r($_SERVER)
It will list everything within the array
它将列出数组中的所有内容
回答by aphoe
you can use getallheaders()to get an array of all HTTP headers sent.
您可以使用它getallheaders()来获取发送的所有 HTTP 标头的数组。
$headers = getallheaders();
foreach($headers as $key=>$val){
echo $key . ': ' . $val . '<br>';
}
回答by Gumbo
Every HTTP request header field is in $_SERVER(except Cookie) and the key begins with HTTP_. If you're using Apache, you can also try apache_request_headers.
每个 HTTP 请求头字段都在$_SERVER(除了Cookie)并且键以HTTP_. 如果您使用的是 Apache,您也可以尝试apache_request_headers.
回答by Panos Kal.
You can simply use apache_request_headers()or its alias getallheaders().
您可以简单地使用apache_request_headers()或其别名getallheaders()。
Usage: echo json_encode(getallheaders());
用法: echo json_encode(getallheaders());
If above function does not exist (old PHP or nginx) you can use this as a fallback:
如果上述函数不存在(旧的 PHP 或 nginx),您可以将其用作后备:
<?php
if (!function_exists('getallheaders')){
function getallheaders() {
$headers = '';
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 davidtbernal
Look at the $_SERVERvariable to see what it contains. The linked manual page has a lot of useful information, but also simply do a var_dumpon it to see what's actually in it. Many of the entries will or won't be filled in, depending on what the client decides to do, and odd quirks of PHP. Looking at the one on my local server, there is also a $_SERVER["ALL_HTTP"] entries that just lists them all as a string, but apparently this isn't standard, as it isn't listed on the manual page.
查看$_SERVER变量以了解它包含的内容。链接的手册页有很多有用的信息,但也只需var_dump对其进行操作即可查看其中的实际内容。许多条目会或不会被填写,这取决于客户端决定做什么,以及 PHP 的奇怪怪癖。看看我本地服务器上的那个,还有一个 $_SERVER["ALL_HTTP"] 条目,只是将它们全部列为一个字符串,但显然这不是标准的,因为它没有列在手册页上。
回答by Eka Hoggy
you can use apache_request_header(); maybe help you.
你可以使用 apache_request_header(); 也许可以帮助你。
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "<pre>";
echo "$header : $value";
echo "</pre>";
}

