Search code examples
php.htaccessauthentication

Retrieve .htaccess login name from PHP


.htaccess is a convenient way to filter accesses to a page, thanks to the Basic authentication for instance.

In this case the

  Require user chandler monica ross rachel

will authorize the access to the page to the 4 persons mentioned above (provided that they enter their appropriate password).

Is it possible in PHP to retrieve the login name that was entered by the user, after she logged in?

For instance chandler logs in and accesses index.php, is it possible to find out that chandler is using the page from the index.php code?


Solution

  • According to manual if you are using basic authentication:

    echo ($_SERVER['PHP_AUTH_USER']);
    

    Basic auth method is deprecated now, I strongly encourage to use digest authentication which is not more difficult. Instead of htpasswd you can use the htdigest binary. There is also an example for that on in the linked manual page.

    Digest also uses the concept of realm. This is the same thing as AuthName you are already using but as basic auth only uses it to show to the user, digest also uses it for authentication purposes and saves it in password files.