Era Host hosting
EraHost – Free Domain, Cheap Hosting!
Client Area
Support 24/7
Menu

How a Programmer Can Solve Issues with apache_request_headers in PHP

3 min read
24.07.2025

The function apache_request_headers() is used to retrieve HTTP headers in PHP, but sometimes it may not work as expected due to server or code issues. Here's a step-by-step guide for programmers to troubleshoot and resolve the problem.

Programmer's Guide to apache_request_headers
Developer-side debugging of apache_request_headers() in PHP.

For the system-administrator-side variant, see Fixing apache_request_headers Authorization Not Working — A System Administrator's Guide; for the Apache-config verification side, Verifying Apache Configuration for apache_request_headers.

This guide is written for developers with programming experience. It includes code examples and server configuration tips.

Check if apache_request_headers() is Available

The apache_request_headers() function is only available when PHP is running as an Apache module. It won't work in other configurations like FastCGI or PHP-FPM.

Solution:

  1. Check if the function exists in your environment:
    if (function_exists('apache_request_headers')) {
        $headers = apache_request_headers();
    } else {
        echo "apache_request_headers is not available.";
    }
  2. If the function isn't available, use the fallback described in Step 4.

Verify Apache Configuration

Sometimes, the server (Apache) may not pass all HTTP headers to PHP, especially the Authorization header.

Solution:

Ask the server administrator (or check yourself) to:

  1. Enable the Apache headers module:
    sudo a2enmod headers
    sudo systemctl restart apache2
  2. Add the following rule to the Apache configuration or .htaccess file:
    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

Check PHP Configuration

The apache_request_headers() function retrieves headers passed from Apache to PHP. If it doesn't work:

Steps:

  1. Check the php.ini configuration file:
    • Locate variables_order:
      variables_order = "EGPCS"
      Ensure the order includes E (Environment).
  2. Restart PHP after making changes:
    sudo systemctl restart php8.0-fpm

Use $_SERVER as a Fallback

If apache_request_headers() doesn't work, you can directly access headers through the $_SERVER superglobal.

Example Code:

// Try apache_request_headers first
if (function_exists('apache_request_headers')) {
    $headers = apache_request_headers();
    $authHeader = $headers['Authorization'] ?? null;
} else {
    // Fallback to $_SERVER
    $authHeader = $_SERVER['HTTP_AUTHORIZATION'] ?? null;
}

// Handle the Authorization header
if ($authHeader) {
    echo "Authorization header: $authHeader";
} else {
    echo "Authorization header not found.";
}
Linux VDS
High performance for your projects
  • Root access and flexible setup
  • Control panel
  • NVMe disks
  • DDR5
Linux VDS

Handle Missing Headers in Reverse Proxies or Load Balancers

If your PHP application runs behind a proxy (e.g., NGINX, AWS ALB), the proxy might strip headers like Authorization.

Solution:

  • For NGINX, add this directive in the NGINX configuration file:
    proxy_set_header Authorization $http_authorization;
  • Restart NGINX:
    sudo systemctl reload nginx

Debugging the Headers

Print all available headers to debug the issue:

$headers = getallheaders(); // Works in all server modes
print_r($headers);

OR

print_r($_SERVER); // Check for HTTP_AUTHORIZATION or other headers

Test the Solution

Use tools like Postman or cURL to test the request and confirm headers are being passed correctly.

cURL Example:

curl -H "Authorization: Bearer YOUR_TOKEN" http://localhost/your-script.php

Key Takeaways for Programmers

  • Use apache_request_headers() when available, but have a fallback plan using $_SERVER.
  • Ensure that the Authorization header or others are passed from the server (Apache, NGINX, or proxies).
  • Debug and print headers to identify what's being received by PHP.
  • If working with modern PHP environments (e.g., PHP-FPM), rely more on $_SERVER than apache_request_headers().

By following these steps, you can handle header-related issues effectively in PHP.

Frequently asked questions
getallheaders() is an alias that works across SAPIs (mod_php, PHP-FPM, ISAPI). apache_request_headers() works only when Apache is the front. Always prefer getallheaders() — it's portable; if you specifically need apache_request_headers() semantics you're tied to one server. For maximum portability, write code that works against $_SERVER directly.
On Apache + PHP-FPM without CGIPassAuth or SetEnvIf — Authorization is stripped before $_SERVER sees it. The fix is server-side (see the sysadmin variant linked above); from PHP alone you can't recover what wasn't forwarded. function_exists('apache_request_headers') returns true under FPM, but the returned array is missing Authorization.
Cloudflare adds CF-Connecting-IP (real client IP) and CF-Visitor (protocol/scheme). $_SERVER['HTTP_CF_CONNECTING_IP'] is the IP your application should treat as "the user"; $_SERVER['REMOTE_ADDR'] will be Cloudflare's edge. Important: only trust CF-* headers if the request actually came from Cloudflare (verify the connecting IP against Cloudflare's published ranges).
Some frameworks normalize header names (Foo-Bar vs foo_bar) and skip headers that don't pass their format check. Authorization, X-Auth-Token, and other custom headers may be silently filtered by middleware. Print the framework's internal header bag (Symfony's $request->headers, Laravel's $request->header) instead of $_SERVER to see what the framework actually exposes.
Related articles
Solving apache_request_headers Issues on NGINX: A Professional Server Administrator's Guide
Verifying Apache Configuration for apache_request_headers
How a Non-Technical User Can Solve apache_request_headers Not Working