Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

The Authorization header never reaches PHP on Apache

Error$_SERVER['HTTP_AUTHORIZATION'] is not set

Short answer

Apache hides the Authorization header from CGI-style scripts by default, so PHP-FPM or CGI never sees it. From Apache 2.4.13 you can allow it with CGIPassAuth On in the directory or .htaccess. Also check that you read the right place: PHP_AUTH_USER is filled only for Basic auth, not for Bearer tokens.

  • Applies to Apache 2.4.13+, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Send a request with a known Authorization header to a PHP file that prints what PHP received. If the header is absent here, the web server removed it before PHP ran.

    # test.php:  <?php print_r(getallheaders()); print_r($_SERVER);
    curl -s -H "Authorization: Bearer test123" http://localhost/test.php | grep -i -E "authorization"
  2. 2Match what you see, then jump to its fix

    1. 1Other headers arrive, but Authorization does not, and PHP runs through FPM or CGIApache withholds the header from CGI-style handlersFix
    2. 2CGIPassAuth is in .htaccess but the header is still missingThe directive is ignored because .htaccess is not allowed to set itFix
    3. 3apachectl -v shows a version below 2.4.13, and the directive is rejected as unknownApache is older than 2.4.13Fix
    4. 4The header does arrive in getallheaders(), but $_SERVER['PHP_AUTH_USER'] is emptyReading a Bearer token from the Basic auth variablesFix

What does the error look like?

  • $_SERVER['HTTP_AUTHORIZATION'] is not set
  • getallheaders() has no Authorization key
  • Bearer token works from Postman against one server but is missing on another

Cause 1 of 4

Apache withholds the header from CGI-style handlers

1Check

The Apache docs say these headers are normally hidden from scripts, so CGIPassAuth defaults to Off. The setting applies to modules such as mod_cgi, mod_cgid and mod_proxy_fcgi.

apachectl -v
grep -rn "CGIPassAuth" /etc/apache2/ .htaccess 2>/dev/null

2Fix

On Apache 2.4.13 or later, turn it on for the directory or in .htaccess. Only do this when your scripts are meant to handle authorization themselves.

<Directory "/var/www/html/api">
    CGIPassAuth On
</Directory>

3Verify

Reload Apache and repeat the test request.

sudo apachectl configtest && sudo systemctl reload apache2
curl -s -H "Authorization: Bearer test123" http://localhost/test.php | grep -i authorization

Confirmed by httpd.apache.org

Did this fix it?

Cause 2 of 4

The directive is ignored because .htaccess is not allowed to set it

1Check

The docs list CGIPassAuth as valid in directory and .htaccess context, with override AuthConfig. If AllowOverride does not include AuthConfig, an .htaccess line cannot take effect.

grep -rn "AllowOverride" /etc/apache2/

2Fix

Allow AuthConfig for that directory, or put the directive in the main config instead of .htaccess.

<Directory "/var/www/html">
    AllowOverride AuthConfig
</Directory>

3Verify

Reload and repeat the test request.

sudo apachectl configtest && sudo systemctl reload apache2

Confirmed by httpd.apache.org

Did this fix it?

Cause 3 of 4

Apache is older than 2.4.13

1Check

The docs say CGIPassAuth exists from 2.4.13. Before that the only built-in option was the compile-time setting SECURITY_HOLE_PASS_AUTHORIZATION.

apachectl -v

2Fix

Upgrade Apache to a current 2.4 release, then use CGIPassAuth. Check mod_rewrite's E= flag (it sets an environment variable) only if an upgrade is impossible.

# upgrade Apache with your operating system package manager

3Verify

Run apachectl -v and confirm 2.4.13 or later.

apachectl -v

Confirmed by httpd.apache.org

Did this fix it?

Cause 4 of 4

Reading a Bearer token from the Basic auth variables

1Check

The PHP manual says only the Basic method is supported and the PHP_AUTH_* variables are set for it. A Bearer token never fills them.

php -r 'echo "use the raw header for Bearer tokens", PHP_EOL;'

2Fix

Read the whole header and split off the scheme yourself.

$h = getallheaders()['Authorization'] ?? '';
if (preg_match('/^Bearer\s+(\S+)$/i', $h, $m)) {
    $token = $m[1];
}

3Verify

Send a Bearer token and print $token.

curl -s -H "Authorization: Bearer test123" http://localhost/api.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Add a tiny diagnostic endpoint to staging that echoes received headers, and test every new server with it.
  • Keep the web server config in version control so CGIPassAuth is not lost on a rebuild.
  • Do not log the Authorization header.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.