Call to undefined function apache_request_headers()

I've just switched my scripts to a different server. On the previous server this worked flawlessly, and now that I've switched them to a different server, I can't understand the problem.

I'm not sure it would help, but here's the relevant code.

$headers = apache_request_headers();

PHP Version is: PHP 5.3.2


Solution 1:

You can use the following replacement function:

<?php
if( !function_exists('apache_request_headers') ) {
///
function apache_request_headers() {
  $arh = array();
  $rx_http = '/\AHTTP_/';
  foreach($_SERVER as $key => $val) {
    if( preg_match($rx_http, $key) ) {
      $arh_key = preg_replace($rx_http, '', $key);
      $rx_matches = array();
      // do some nasty string manipulations to restore the original letter case
      // this should work in most cases
      $rx_matches = explode('_', $arh_key);
      if( count($rx_matches) > 0 and strlen($arh_key) > 2 ) {
        foreach($rx_matches as $ak_key => $ak_val) $rx_matches[$ak_key] = ucfirst($ak_val);
        $arh_key = implode('-', $rx_matches);
      }
      $arh[$arh_key] = $val;
    }
  }
  return( $arh );
}
///
}
///
?>

Source: PHP Manual

Solution 2:

From the docs, before the release of PHP 5.4.0:

This function is only supported when PHP is installed as an Apache module.

PHP 5.4.0 and later support this function unconditionally.

Said docs also include replacement functions that mimic the functionality of apache_request_headers by stepping through $_SERVER.

Solution 3:

if php is installed as an Apache module:

apache_request_headers()["Authorization"];

else, go to .htaccess file and add:

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

You can then access request headers using any of these:

$_SERVER["HTTP_AUTHORIZATION"]; // using super global

OR

$app->request->headers("Authorization"); // if using PHP Slim