How do I enable PHP apache_request_headers() or change PHP into an apache module

inac asked:

PHP, WHM, and several other services are already installed on a CentOS x64 server I am trying to migrate data to. Many of my existing PHP scripts are dependent on PHP’s apache_request_headers() function, which the current server’s PHP configuration does not support. Apparently, compiling PHP as an Apache module is one solution, but are there other ways to enable this (without uninstalling PHP, reinstalling, etc., and all dependent services), perhaps as easy as modifying php.ini, somehow?

My answer:

If you’re running PHP as CGI/FastCGI then you don’t get the apache_request_headers() (also known as getallheaders()) function.


As of 5.4.0, PHP includes a version of this function which works in FastCGI.

For previous versions of PHP you can roll your own. Though this approach loses case sensitivity for header names (CGI/FastCGI capitalize all header names) it’s generally good enough for 99.9% of possible purposes.

This is my code for doing so, written long ago and published under LGPLv3:

if (!is_callable('getallheaders')) {
    # Convert a string to mixed-case on word boundaries.
    function uc_all($string) {
        $temp = preg_split('/(W)/', str_replace("_", "-", $string), -1, PREG_SPLIT_DELIM_CAPTURE);
        foreach ($temp as $key=>$word) {
            $temp[$key] = ucfirst(strtolower($word));
        return join ('', $temp);

    function getallheaders() {
        $headers = array();
        foreach ($_SERVER as $h => $v)
            if (preg_match('/HTTP_(.+)/', $h, $hp))
                $headers[str_replace("_", "-", uc_all($hp[1]))] = $v;
        return $headers;

    function apache_request_headers() { return getallheaders(); }

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.