The Authorization header never reaches PHP on Apache
$_SERVER['HTTP_AUTHORIZATION'] is not setShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
Other headers arrive, but Authorization does not, and PHP runs through FPM or CGIApache withholds the header from CGI-style handlersFix - 2
CGIPassAuth is in .htaccess but the header is still missingThe directive is ignored because .htaccess is not allowed to set itFix - 3
apachectl -v shows a version below 2.4.13, and the directive is rejected as unknownApache is older than 2.4.13Fix - 4
The header does arrive in getallheaders(), but $_SERVER['PHP_AUTH_USER'] is emptyReading a Bearer token from the Basic auth variablesFix
- 1
What does the error look like?
$_SERVER['HTTP_AUTHORIZATION'] is not setgetallheaders() has no Authorization keyBearer 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/null2Fix
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 authorizationConfirmed by httpd.apache.org
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 apache2Confirmed by httpd.apache.org
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 -v2Fix
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 manager3Verify
Run apachectl -v and confirm 2.4.13 or later.
apachectl -vConfirmed by httpd.apache.org
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.phpConfirmed by php.net
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix