In PHP, if you want to get all HTTP request headers, you can use the getallheaders method, but this method does not exist in any environment. For example, if you use fastcgi to run PHP, there is no such method, so we still We need to consider other methods. Fortunately, there is something we want in $_SERVER. The key name in it starts with HTTP_ is the HTTP request header: the code is very simple, it needs to be explained that the name of the message header is clearly indicated in the RFC It is not case sensitive. However, not all HTTP request headers exist in $_SERVER in the form of keys beginning with HTTP_. For example, Authorization, Content-Length, Content-Type are not the case, so in order to obtain all HTTP request headers, Also need to add the following piece of code: $headers = array();
foreach ($_SERVER as $key => $value) {
if ('HTTP_' == substr($key, 0, 5)) {
$headers[str_replace('_', '-', substr($key, 5))] = $value;
}
}if (isset($_SERVER['PHP_AUTH_DIGEST'])) {
$header['AUTHORIZATION'] = $_SERVER['PHP_AUTH_DIGEST']);
} elseif (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
$header['AUTHORIZATION'] = base64_encode($_SERVER['PHP_AUTH_USER'] . ':' .$_SERVER['PHP_AUTH_PW']));
}
if (isset($_SERVER['CONTENT_LENGTH'])) {
$header['CONTENT-LENGTH'] = $_SERVER['CONTENT_LENGTH'];
}
if (isset($_SERVER['CONTENT_TYPE'])) {
$header['CONTENT-TYPE'] = $_SERVER['CONTENT_TYPE'];
}
Use PHP to get all HTTP request headers
Guess you like
Origin blog.csdn.net/ld17822307870/article/details/113878464
Ranking