apache_request_headers() function in PHP is not returning the Authorization header.
This is typically due to a configuration issue in Apache, PHP, or the server environment. Here's how a system administrator would troubleshoot and solve this issue step by step.
Verify apache_request_headers is Enabled
- The function
apache_request_headersis only available if the Apache module is properly installed and PHP is running as an Apache module (not CGI or FPM).
Steps to Verify:
- Check if Apache headers module is enabled:
apachectl -M | grep headersLook for
sudo a2enmod headersheaders_module (shared)in the output. If it's not present, enable it:
sudo systemctl restart apache2 - If you're using PHP-FPM or running PHP as CGI,
apache_request_headersmight not work. Use$_SERVER['HTTP_AUTHORIZATION']instead (explained in Step 4).
Ensure Apache Passes Authorization Headers
In some configurations, Apache strips the Authorization header before passing the request to PHP.
Steps to Fix:
- Open your Apache configuration file (e.g.,
httpd.conf,apache2.conf, or the specific virtual host configuration file). - Ensure the following directive is present:
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 - Restart Apache:
sudo systemctl restart apache2This ensures the Authorization header is preserved and made available to PHP.
Check .htaccess Rules
If your server uses .htaccess files, ensure that no rules are stripping headers.
Steps:
- Open the
.htaccessfile in the root directory of your application. - Add the following lines (or ensure they are present):
RewriteEngine On RewriteCond %{HTTP:Authorization} ^(.*) RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1] - Save the file and restart Apache:
sudo systemctl restart apache2
Use $_SERVER as an Alternative
If apache_request_headers is still not working, you can access the Authorization header directly from the $_SERVER superglobal.
PHP Code Example:
Add the following fallback logic in your application:
<?php
if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
$authHeader = $_SERVER['HTTP_AUTHORIZATION'];
} elseif (function_exists('apache_request_headers')) {
$headers = apache_request_headers();
$authHeader = $headers['Authorization'] ?? null;
} else {
$authHeader = null;
}
if ($authHeader) {
echo "Authorization header: " . $authHeader;
} else {
echo "Authorization header not found.";
}
?>
Debug with Logs
Enable detailed logging in Apache to verify if the Authorization header is being received from the client and passed to PHP.
Steps to Enable Logging:
- Open the virtual host file or
apache2.conf. - Add or modify the LogLevel directive:
LogLevel debug - Check the logs after restarting Apache:
sudo tail -f /var/log/apache2/error.logLook for the incoming Authorization header to confirm if Apache is receiving it.
Check Proxy or Load Balancer Settings
If your server is behind a proxy or load balancer (e.g., NGINX), the Authorization header may be stripped before reaching Apache.
For NGINX:
- Open the NGINX configuration file (e.g.,
/etc/nginx/nginx.confor the specific site configuration file). - Add the following directive:
proxy_set_header Authorization $http_authorization; - Reload NGINX:
sudo systemctl reload nginx
Verify PHP Configuration
Some PHP configurations may interfere with headers. Verify that no restrictive settings are in place.
Steps:
- Check the
php.inifile for any header-related directives:
sudo nano /etc/php/8.0/apache2/php.iniLook for:
variables_order = "EGPCS"Ensure E (Environment) is included, as it allows Apache to pass environment variables like Authorization.
- Restart PHP and Apache:
sudo systemctl restart php8.0-fpm
sudo systemctl restart apache2
Test the Solution
- Make a request to your application with an Authorization header using tools like Postman or curl:
curl -H "Authorization: Bearer YOUR_TOKEN" http://yourserver.com/api - Check if the application receives the Authorization header properly.
Final Checklist
- Ensure the
headers_moduleis enabled in Apache. - Preserve the Authorization header using
SetEnvIfor.htaccess. - Use
$_SERVER['HTTP_AUTHORIZATION']as a fallback in PHP. - Check and configure proxies or load balancers to pass the Authorization header.
- Debug logs to verify the flow of headers.
By following these steps, the apache_request_headers Authorization not working issue can be resolved.


