2

.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?

Déjà vu
  • 28,223
  • 6
  • 72
  • 100
  • http://php.net/manual/en/features.http-auth.php might be worth a look, though I'm not exactly sure that it's what you're after. – GordonM Mar 08 '11 at 10:18

4 Answers4

7

According to manual if you are using basic authentication:

echo ($_SERVER['PHP_AUTH_USER']);

Basic auth method is deprecated now, I sorongly 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.

vbence
  • 20,084
  • 9
  • 69
  • 118
1

Depends. eg. if your server's auth scheme is

AuthMySQL On

then the user name is in

$_SERVER['REMOTE_USER']
yPhil
  • 8,049
  • 4
  • 57
  • 83
1

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

Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
0

$_SERVER['REMOTE_USER'] seems to work with php 7.3. One way to find out is to run a php script from within the protected folder. Use print_r($_SERVER) and look at the results. If you find the logged in name, you'll know which $_SERVER option is available.

Das_Geek
  • 2,775
  • 7
  • 20
  • 26