Last active
December 15, 2015 20:29
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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