Inspect and save apache request headers

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.

  1. <?php
  2.  
  3. $log_file = "header_log.txt";
  4.  
  5. if(!function_exists('apache_request_headers')) {
  6.     function apache_request_headers() {
  7.         $headers = array();
  8.         foreach($_SERVER as $key => $value) {
  9.             if(substr($key, 0, 5) == 'HTTP_') {
  10.                 $headers[str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value;
  11.             }
  12.         }
  13.         return $headers;
  14.     }
  15. }
  16.  
  17. $headers = apache_request_headers();
  18.  
  19. foreach ($headers as $header => $value) {
  20.     exec("echo '$header: $value \n' >> $log_file");
  21. }
  22.  
  23. ?>