PHP how to get HTTP request header information, is an urgent solution and do not know how to choose the problem, I collected and collated, for reference
The PHP manual provides ready-made functions:
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 theapache_request_headers () documentation for more information on how the This function works.
return value
An associative array of all the HTTP headers in the "current request", Orfalse on failure.
Example #1 getallheaders () Example
Copy CodeThe code is as follows:
<?php
foreach (Getallheaders () as $name = = $value) {
echo "$name: $value \ n";
}
?>
However, this function can only be used in Apache environment, IIS or Nginx is not supported, can be implemented by custom functions
Copy CodeThe code is 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, let's see what's printed out.
Copy CodeThe code is as follows:
<?php
Print_r (Getallheaders ());
Get results:
Copy CodeThe code is 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
)
PHP Get header Information implementation steps for HTTP request