How should I read any header in PHP?
For example the custom header: X-Requested-With
.
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):
<?php
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
ELSE: In any other case, you can use (userland implementation):
<?php
function getRequestHeaders() {
$headers = array();
foreach($_SERVER as $key => $value) {
if (substr($key, 0, 5) <> 'HTTP_') {
continue;
}
$header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
$headers[$header] = $value;
}
return $headers;
}
$headers = getRequestHeaders();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
See Also:
getallheaders() - (PHP >= 5.4) cross platform edition Alias of apache_request_headers()
apache_response_headers() - Fetch all HTTP response headers.
headers_list() - Fetch a list of headers to be sent.
$_SERVER['HTTP_X_REQUESTED_WITH']
RFC3875, 4.1.18:
Meta-variables with names beginning with
HTTP_
contain values read from the client request header fields, if the protocol used is HTTP. The HTTP header field name is converted to upper case, has all occurrences of-
replaced with_
and hasHTTP_
prepended to give the meta-variable name.
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:
$headers = array();
foreach ($_SERVER as $key => $value) {
if (strpos($key, 'HTTP_') === 0) {
$headers[str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value;
}
}
Now just use $headers['XRequestedWith']
to retrieve the desired header.
PHP manual on $_SERVER
: http://php.net/manual/en/reserved.variables.server.php
Since PHP 5.4.0 you can use getallheaders
function which returns all request headers as an associative array:
var_dump(getallheaders());
// array(8) {
// ["Accept"]=>
// string(63) "text/html[...]"
// ["Accept-Charset"]=>
// string(31) "ISSO-8859-1[...]"
// ["Accept-Encoding"]=>
// string(17) "gzip,deflate,sdch"
// ["Accept-Language"]=>
// string(14) "en-US,en;q=0.8"
// ["Cache-Control"]=>
// string(9) "max-age=0"
// ["Connection"]=>
// string(10) "keep-alive"
// ["Host"]=>
// string(9) "localhost"
// ["User-Agent"]=>
// string(108) "Mozilla/5.0 (Windows NT 6.1; WOW64) [...]"
// }
Earlier this function worked only when PHP was running as an Apache/NSAPI module.
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:
if (!function_exists('getallheaders')) {
foreach ($_SERVER as $name => $value) {
/* RFC2616 (HTTP/1.1) defines header fields as case-insensitive entities. */
if (strtolower(substr($name, 0, 5)) == 'http_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
$this->request_headers = $headers;
} else {
$this->request_headers = getallheaders();
}
Notice the subtle differences with previous suggestions. The function here also works on php-fpm (+nginx).
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;
}
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.
To make things simple, here is how you can get just the one you want:
Simple:
$headerValue = $_SERVER['HTTP_X_REQUESTED_WITH'];
or when you need to get one at a time:
<?php
/**
* @param $pHeaderKey
* @return mixed
*/
function get_header( $pHeaderKey )
{
// Expanded for clarity.
$headerKey = str_replace('-', '_', $pHeaderKey);
$headerKey = strtoupper($headerKey);
$headerValue = NULL;
// Uncomment the if when you do not want to throw an undefined index error.
// I leave it out because I like my app to tell me when it can't find something I expect.
//if ( array_key_exists($headerKey, $_SERVER) ) {
$headerValue = $_SERVER[ $headerKey ];
//}
return $headerValue;
}
// X-Requested-With mainly used to identify Ajax requests. Most JavaScript frameworks
// send this header with value of XMLHttpRequest, so this will not always be present.
$header_x_requested_with = get_header( 'X-Requested-With' );
The other headers are also in the super global array $_SERVER, you can read about how to get at them here: http://php.net/manual/en/reserved.variables.server.php
Here's how I'm doing it. You need to get all headers if $header_name isn't passed:
<?php
function getHeaders($header_name=null)
{
$keys=array_keys($_SERVER);
if(is_null($header_name)) {
$headers=preg_grep("/^HTTP_(.*)/si", $keys);
} else {
$header_name_safe=str_replace("-", "_", strtoupper(preg_quote($header_name)));
$headers=preg_grep("/^HTTP_${header_name_safe}$/si", $keys);
}
foreach($headers as $header) {
if(is_null($header_name)){
$headervals[substr($header, 5)]=$_SERVER[$header];
} else {
return $_SERVER[$header];
}
}
return $headervals;
}
print_r(getHeaders());
echo "\n\n".getHeaders("Accept-Language");
?>
It looks a lot simpler to me than most of the examples given in other answers. This also gets the method (GET/POST/etc.) and the URI requested when getting all of the headers which can be useful if you're trying to use it in logging.
Here's the output:
Array ( [HOST] => 127.0.0.1 [USER_AGENT] => Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0 [ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 [ACCEPT_LANGUAGE] => en-US,en;q=0.5 [ACCEPT_ENCODING] => gzip, deflate [COOKIE] => PHPSESSID=MySessionCookieHere [CONNECTION] => keep-alive )
en-US,en;q=0.5
This work if you have an Apache server
PHP Code:
$headers = apache_request_headers();
foreach ($headers as $header => $value) {
echo "$header: $value <br />\n";
}
Result:
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0
Host: www.example.com
Connection: Keep-Alive