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
<?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
<?php 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; } }?>
All right, let's see what's printed out.
<?php Print_r (Getallheaders ());
Get results:
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)
Related articles:
Php method for getting header information for HTTP requests
PHP HTTP request class, support Get,post,multipart/form-data
PHP implementation of HTTP request Encapsulation example