-
- $headers = array();
- foreach ($_SERVER as $key => $value) {
- if ('HTTP_' == substr($key, 0, 5)) {
- $headers[str_replace('_', '-', substr($key, 5))] = $value;
- }
- }
- ?>
-
Copy code
Explanation: Clear in RFC Pointed out that header names are not case-sensitive.
However, not all HTTP request headers exist in $_SERVER in the form of keys starting with HTTP_. For example, Authorization, Content-Length, and Content-Type are not like this, so in order to obtain all HTTP request headers , you also need to add the following code:
-
- 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'];
- }
- ?>
Copy code
|