The PHP function apache_request_headers() relies on Apache properly passing HTTP headers to the PHP environment. If it doesn't work, it's often due to Apache configuration issues. Here's how to verify and fix your Apache setup.
Ensure the Apache Headers Module is Enabled
The mod_headers module must be enabled in Apache to pass and manipulate HTTP headers.
Check if mod_headers is Enabled
- Run the following command:
apachectl -M | grep headers- If you see
headers_module (shared)in the output, the module is enabled. - If not, enable it.
- If you see
Enable mod_headers
- Enable the module:
sudo a2enmod headers - Restart Apache:
sudo systemctl restart apache2
Ensure Headers Are Passed to PHP
Some Apache configurations strip certain headers (like Authorization) before passing the request to PHP.
Modify Apache Configuration
- Open the main Apache configuration file:
sudo nano /etc/apache2/apache2.confOr, if using a virtual host:
sudo nano /etc/apache2/sites-available/your-site.conf - Add the following directive inside the
<VirtualHost>block or globally:SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1 - Save the file and restart Apache:
sudo systemctl restart apache2
Check .htaccess Rules
If your application uses an .htaccess file, it might be stripping headers.
Add Header Rules to .htaccess
- Open your
.htaccessfile in the root directory of your application. - Add the following:
RewriteEngine On RewriteCond %{HTTP:Authorization} ^(.*) RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1] - Save the file.
Check for Proxy or Load Balancer Issues
If your Apache server is behind a reverse proxy or load balancer (e.g., NGINX, Cloudflare), the proxy might strip headers before passing them to Apache.
Fix for NGINX:
- Open the NGINX configuration file (e.g.,
/etc/nginx/nginx.confor site-specific config). - Add this directive to preserve the Authorization header:
proxy_set_header Authorization $http_authorization; - Reload NGINX:
sudo systemctl reload nginx
Debug Apache Header Handling
To verify that Apache is correctly passing headers to PHP:
Steps:
- Enable Apache logging at a higher level. Open the Apache config file:
sudo nano /etc/apache2/apache2.conf - Set the LogLevel to debug:
LogLevel debug - Restart Apache:
sudo systemctl restart apache2 - Tail the logs and look for header-related messages:
sudo tail -f /var/log/apache2/error.log
Test Header Passing
Use a tool like curl or Postman to send a request to your server and check the headers.
cURL Example:
curl -H "Authorization: Bearer YOUR_TOKEN" http://your-server.com/your-script.php
Debug in PHP:
Create a simple test script (headers.php) to see all the headers:
<?php
print_r(apache_request_headers());
?>
Checklist to Ensure Proper Apache Configuration
- Enable
mod_headers. - Ensure Authorization headers are preserved using
SetEnvIf. - Check
.htaccessrules for any conflicting directives. - Fix proxy settings to pass headers if a reverse proxy is used.
- Enable logging to debug header behavior in Apache.
By verifying and fixing these aspects of Apache configuration, the apache_request_headers() function should work as expected.


