Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 387
Déjà vu Avatar asked Feb 09 '26 15:02

Déjà vu


2 Answers

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.

like image 140
vbence Avatar answered Feb 12 '26 14:02

vbence


$_SERVER['PHP_AUTH_USER'] might do the trick.

like image 25
Tatu Ulmanen Avatar answered Feb 12 '26 15:02

Tatu Ulmanen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!