The PHP manual provides a ready-made function:
Getallheaders
(PHP 4, PHP 5)
Getallheaders-fetch all HTTP Request headers
Description
Array getallheaders (void)
Fetches all HTTP headers from the current request.
This function is a alias for Apache_request_headers (). Please read the theapache_request_headers () documentation for more information on the How this function works.
return value
An associative array of ' all ' HTTP headers in the ' current ' request, orfalse on failure.
Example #1 getallheaders () Example
Copy Code code as follows:
<?php
foreach (Getallheaders () as $name => $value) {
echo "$name: $value \ n";
}
?>
However, this function can only be used in the Apache environment, which is not supported by IIS or Nginx, and can be implemented by a custom function
Copy Code code as follows:
<?php
<span class=html>if (!function_exists (' getallheaders '))
{
function Getallheaders ()
{
foreach ($_server as $name => $value)
{
if (substr ($name, 0, 5) = = ' Http_ ')
{
$headers [Str_replace (', '-', Ucwords (Strtolower) (Str_replace (' _ ', ', substr ($name, 5)))] = $value;
}
}
return $headers;
}
}</span>
?>
All right, look what's printed out.
Copy Code code as follows:
<?php
Print_r (Getallheaders ());
Get results:
Copy Code code as follows:
Array
(
[Accept] => */*
[Accept-language] => ZH-CN
[accept-encoding] => gzip, deflate
[User-agent] => mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; trident/4.0. NET CLR 2.0.50727)
[Host] => localhost
[Connection] => keep-alive
)