PHP/MariaDB/MySQL

HTTP Authentication error in PHP

Updated November 2026 12,326 views 3 min read
PHP/MySQL | Updated November 2026

If your PHP script uses HTTP authentication ($_SERVER['PHP_AUTH_USER']) and it is not working on your hosting account, the issue is almost certainly that Apache is not passing the authentication headers through to PHP. This is common on shared hosting where PHP runs as CGI or LSAPI rather than as an Apache module.

01. Why HTTP Auth Fails on Shared Hosting

When PHP runs as an Apache module (mod_php), it has direct access to the Authorization header that browsers send during HTTP authentication. The $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] variables are populated automatically.

On Ultra Web Hosting (and most modern shared hosting), PHP runs via LSAPI or CGI, not as an Apache module. In these configurations, Apache strips the Authorization header before passing the request to PHP for security reasons. The result: $_SERVER['PHP_AUTH_USER'] is empty and your authentication code never triggers.

02. Fix: Pass Auth Headers via .htaccess

Add these lines to the .htaccess file in the directory where your PHP script lives:

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

This tells Apache to capture the Authorization header and store it in an environment variable called HTTP_AUTHORIZATION, which PHP can then access.

If your site already has RewriteEngine enabled and other rewrite rules, just add the RewriteRule line after RewriteEngine On and before any other rules.

For more on .htaccess, see our Complete Guide to .htaccess.

03. Reading the Header in PHP

After adding the .htaccess rule, update your PHP code to read the header from the environment variable and decode it:

<?php
// Check for the Authorization header in multiple possible locations
$auth = null;
if (isset($_SERVER['HTTP_AUTHORIZATION'])) {
    $auth = $_SERVER['HTTP_AUTHORIZATION'];
} elseif (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) {
    $auth = $_SERVER['REDIRECT_HTTP_AUTHORIZATION'];
}

if ($auth && strpos($auth, 'Basic') === 0) {
    $decoded = base64_decode(substr($auth, 6));
    list($user, $pass) = explode(':', $decoded, 2);
    // $user and $pass now contain the credentials
}
?>
Tip

The variable may appear as either HTTP_AUTHORIZATION or REDIRECT_HTTP_AUTHORIZATION depending on how Apache processes the rewrite. Always check both. If you are using a framework like Laravel or Symfony, this fix is usually handled automatically by the framework's .htaccess rules.

04. Alternative: Use .htpasswd for Directory Protection

If you just need to password-protect a directory or a set of pages (and do not need to handle authentication in PHP code), cPanel's built-in directory protection is simpler and works regardless of the PHP handler:

See How to Password Protect a Directory for the step-by-step guide. This approach uses Apache's native .htpasswd authentication, which works at the web server level before PHP is even invoked.

Need Help With PHP Authentication?

If your authentication setup still is not working after the .htaccess fix, open a ticket with your script details and we will take a look.

Open a Support Ticket

Frequently Asked Questions

Why does PHP HTTP authentication not work on my hosting?

Because PHP runs as CGI or LSAPI on shared hosting rather than as an Apache module, so Apache does not pass the HTTP Authorization header to PHP by default. That leaves $_SERVER['PHP_AUTH_USER'] empty. You fix it by forwarding the header in .htaccess.

How do I pass the auth header to PHP via .htaccess?

Add a rewrite rule that copies the Authorization header into the environment, for example a RewriteRule using [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]. With the header forwarded, PHP_AUTH_USER and PHP_AUTH_PW populate correctly.

How do I read the credentials in PHP after fixing it?

Read $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] once the header is forwarded. If they are still empty, check the rewrite rule and that mod_rewrite is active, then confirm the browser actually sent credentials.

Is there a simpler alternative to PHP HTTP auth?

Yes. For basic password protection of a directory, use cPanel's Directory Privacy (.htpasswd) instead of coding HTTP auth in PHP. It is simpler, handled by Apache, and avoids the header-forwarding issue entirely.

Quick Recap

  1. PHP_AUTH_USER is empty on shared hosting - PHP runs as LSAPI/CGI, not mod_php
  2. Add a RewriteRule to .htaccess to pass the Authorization header through
  3. Read from HTTP_AUTHORIZATION or REDIRECT_HTTP_AUTHORIZATION in PHP
  4. Decode the Base64 value to extract username and password
  5. Consider .htpasswd instead if you just need simple directory protection

PHP development on shared hosting · Last updated November 2026 · Browse all PHP/MySQL articles

Share: X in Reddit f Email
  • 194 Users Found This Useful

Was this answer helpful?

Related Articles

How to Manage Your Database with phpMyAdmin

PHP & MySQL | Updated July 2026 phpMyAdmin is the web tool every Ultra Web Hosting...

How can I view/modify data in my MySQL database?

PHP/MySQL | Updated November 2026 phpMyAdmin is the primary tool for viewing, editing, and...

Server Paths - PHP Perl Python and More

PHP/MariaDB/MySQL | Updated November 2026 The default path to PHP on Ultra Web Hosting...

Disable MySQL strict mode on cPanel server

Databases | Updated January 2026 MySQL/MariaDB strict mode enforces stricter data validation...

How to Parse HTML as PHP

PHP/MariaDB/MySQL | Updated November 2026 By default, Apache only processes files with a .php...



Save 30% on web hosting - Use coupon code Hosting30