IF: you only need a single header, instead of all headers, the quickest method is:
<?php
// Replace XXXXXX_XXXX with the name of the header you need in UPPERCASE (and with '-' replaced by '_')
$headerStringValue = $_SERVER['HTTP_XXXXXX_XXXX'];
ELSE IF: you run PHP as an Apache module or, as of PHP 5.4, using FastCGI (simple method):
You should find all HTTP headers in the $_SERVER global variable prefixed with HTTP_ uppercased and with dashes (-) replaced by underscores (_).
For instance your X-Requested-With can be found in:
$_SERVER['HTTP_X_REQUESTED_WITH']
It might be convenient to create an associative array from the $_SERVER variable. This can be done in several styles, but here's a function that outputs camelcased keys:
Pass a header name to this function to get its value without using for loop. Returns null if header not found.
/**
* @var string $headerName case insensitive header name
*
* @return string|null header value or null if not found
*/
function get_header($headerName)
{
$headers = getallheaders();
return isset($headerName) ? $headers[$headerName] : null;
}
strtolower is lacking in several of the proposed solutions, RFC2616 (HTTP/1.1) defines header fields as case-insensitive entities. The whole thing, not just the value part.
So suggestions like only parsing HTTP_ entries are wrong.
IF: you only need a single header, instead of all headers, the quickest method is:
ELSE IF: you run PHP as an Apache module or, as of PHP 5.4, using FastCGI (simple method):
apache_request_headers()
ELSE: In any other case, you can use (userland implementation):
See Also:
getallheaders() - (PHP >= 5.4)
cross platform editionAlias ofapache_request_headers()
apache_response_headers() - Fetch all HTTP response headers.headers_list() - Fetch a list of headers to be sent.
You should find all HTTP headers in the
$_SERVER
global variable prefixed withHTTP_
uppercased and with dashes (-) replaced by underscores (_).For instance your
X-Requested-With
can be found in:It might be convenient to create an associative array from the
$_SERVER
variable. This can be done in several styles, but here's a function that outputs camelcased keys:Now just use
$headers['XRequestedWith']
to retrieve the desired header.PHP manual on
$_SERVER
: http://php.net/manual/en/reserved.variables.server.phpPass a header name to this function to get its value without using
for
loop. Returns null if header not found.Note: this works only with Apache server, see: http://php.net/manual/en/function.getallheaders.php
Note: this function will process and load all of the headers to the memory and it's less performant than a
for
loop.I use this function to get the custom headers, if the header starts from "HTTP_X_" we push in the array :)
strtolower
is lacking in several of the proposed solutions, RFC2616 (HTTP/1.1) defines header fields as case-insensitive entities. The whole thing, not just the value part.So suggestions like only parsing HTTP_ entries are wrong.
Better would be like this:
Notice the subtle differences with previous suggestions. The function here also works on php-fpm (+nginx).
if only one key is required to retrieved, For example
"Host"
address is required, then we can useSo that we can avoid loops and put it inline to the echo outputs