Inspect and save apache request headers

Submitted by Dan on Tue, 04/22/2014 - 13:07

I wrote this function because one of our web apps that was communicating with our web server suddenly began receiving Error 406: Not Acceptable (The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request). So I inserted this bit of code to capture exactly what that accept header was and see if we could fix the problem. It writes the output to a file on the webserver.

<?php
 
$log_file = "header_log.txt";
 
if(!function_exists('apache_request_headers')) {
    function apache_request_headers() {
        $headers = array();
        foreach($_SERVER as $key => $value) {
            if(substr($key, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value;
            }
        }
        return $headers;
    }
}
 
$headers = apache_request_headers();
 
foreach ($headers as $header => $value) {
    exec("echo '$header: $value \n' >> $log_file");
}
 
?>