Skip to content

Instantly share code, notes, and snippets.

@pjdietz
Last active December 15, 2015 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjdietz/5319239 to your computer and use it in GitHub Desktop.
Save pjdietz/5319239 to your computer and use it in GitHub Desktop.
Static class to facilitate looking up request headers. The class allows you to look up request headers case insensitively.
<?php
/**
* Class RequestHeaders
*
* Static class to facilitate looking up request headers.
*/
class RequestHeaders
{
/**
* Headers from apache_request_headers with keys normalize to lowercase.
*
* @var array
*/
static private $headers;
/**
* Return the value of the header with the given field name. If not set,
* return null.
*
* @param string $headerField
* @return string|null
*/
public static function getHeader($headerField)
{
if (!isset(self::$headers)) {
self::readHeaders();
}
// The keys in the headers array are normalized to lowercase, so
// normalize the argument to match.
$headerField = strtolower($headerField);
if (isset(self::$headers[$headerField])) {
return self::$headers[$headerField];
}
return null;
}
/**
* Store headers returned by apache_request_headers() with the field
* names normalized to lowercase.
*/
private static function readHeaders()
{
// Copy the list of headers and normalize the case of the field names.
$headers = apache_request_headers();
self::$headers = array();
foreach ($headers as $k => $v) {
self::$headers[strtolower($k)] = $v;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment