我应该如何阅读任何头在PHP?
例如自定义报头:X-Requested-With。
我应该如何阅读任何头在PHP?
例如自定义报头:X-Requested-With。
当前回答
为了让事情变得简单,以下是如何得到你想要的:
简单:
$headerValue = $_SERVER['HTTP_X_REQUESTED_WITH'];
或者当你需要一次得到一个:
<?php
/**
* @param $pHeaderKey
* @return mixed
*/
function get_header( $pHeaderKey )
{
// Expanded for clarity.
$headerKey = str_replace('-', '_', $pHeaderKey);
$headerKey = strtoupper($headerKey);
$headerValue = NULL;
// Uncomment the if when you do not want to throw an undefined index error.
// I leave it out because I like my app to tell me when it can't find something I expect.
//if ( array_key_exists($headerKey, $_SERVER) ) {
$headerValue = $_SERVER[ $headerKey ];
//}
return $headerValue;
}
// X-Requested-With mainly used to identify Ajax requests. Most JavaScript frameworks
// send this header with value of XMLHttpRequest, so this will not always be present.
$header_x_requested_with = get_header( 'X-Requested-With' );
其他的头文件也在超级全局数组$_SERVER中,你可以在这里阅读如何获取它们:http://php.net/manual/en/reserved.variables.server.php
其他回答
如果您有一台Apache服务器,这是可行的
PHP代码:
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
结果:
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Host: www.example.com
Connection: Keep-Alive
如果:你只需要一个头文件,而不是所有的头文件,最快的方法是:
<?php
// Replace XXXXXX_XXXX with the name of the header you need in UPPERCASE (and with '-' replaced by '_')
$headerStringValue = $_SERVER['HTTP_XXXXXX_XXXX'];
ELSE IF:你将PHP作为Apache模块运行,或者在php5.4中使用FastCGI(简单方法):
apache_request_headers ()
<?php
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
ELSE:在任何其他情况下,您可以使用(用户域实现):
<?php
function getRequestHeaders() {
$headers = array();
foreach($_SERVER as $key => $value) {
if (substr($key, 0, 5) <> 'HTTP_') {
continue;
}
$header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
$headers[$header] = $value;
}
return $headers;
}
$headers = getRequestHeaders();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
参见: getallheaders() (PHP >= 5.4)跨平台版本apache_request_headers()的别名 apache_response_headers() -获取所有HTTP响应头。 headers_list() -获取要发送的报头列表。
$_SERVER['HTTP_X_REQUESTED_WITH']
RFC3875 4.1.18:
名称以HTTP_开头的元变量包含从客户端请求报头字段读取的值,如果使用的协议是HTTP。HTTP报头字段名被转换为大写,所有出现的-都替换为_,并在前面加上HTTP_来给出元变量名。
为了让事情变得简单,以下是如何得到你想要的:
简单:
$headerValue = $_SERVER['HTTP_X_REQUESTED_WITH'];
或者当你需要一次得到一个:
<?php
/**
* @param $pHeaderKey
* @return mixed
*/
function get_header( $pHeaderKey )
{
// Expanded for clarity.
$headerKey = str_replace('-', '_', $pHeaderKey);
$headerKey = strtoupper($headerKey);
$headerValue = NULL;
// Uncomment the if when you do not want to throw an undefined index error.
// I leave it out because I like my app to tell me when it can't find something I expect.
//if ( array_key_exists($headerKey, $_SERVER) ) {
$headerValue = $_SERVER[ $headerKey ];
//}
return $headerValue;
}
// X-Requested-With mainly used to identify Ajax requests. Most JavaScript frameworks
// send this header with value of XMLHttpRequest, so this will not always be present.
$header_x_requested_with = get_header( 'X-Requested-With' );
其他的头文件也在超级全局数组$_SERVER中,你可以在这里阅读如何获取它们:http://php.net/manual/en/reserved.variables.server.php
RFC2616 (HTTP/1.1)将报头字段定义为不区分大小写的实体。整个事情,而不仅仅是价值部分。
所以像只解析HTTP_条目这样的建议是错误的。
更好的是这样的:
if (!function_exists('getallheaders')) {
foreach ($_SERVER as $name => $value) {
/* RFC2616 (HTTP/1.1) defines header fields as case-insensitive entities. */
if (strtolower(substr($name, 0, 5)) == 'http_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
$this->request_headers = $headers;
} else {
$this->request_headers = getallheaders();
}
注意与前面建议的细微差别。这里的函数也适用于php-fpm (+nginx)。