How can I access the apache basic-auth username in a PHP script?
Simply access this variable:
$_SERVER['PHP_AUTH_USER']
HTTP Auth in the PHP Manual would be a good starting place.
Check what you have in $_SERVER (print_r($_SERVER)), sometimes $_SERVER['PHP_AUTH_USER'] is not available.
If you have value for $_SERVER["HTTP_AUTHORIZATION"], you may use:
if (isset($_SERVER["HTTP_AUTHORIZATION"]) {
$auth = $_SERVER["HTTP_AUTHORIZATION"];
$auth_array = explode(" ", $auth);
$un_pw = explode(":", base64_decode($auth_array[1]));
$un = $un_pw[0];
$pw = $un_pw[1];
}
(copied from https://www.codepunker.com/blog/php-a-primer-on-the-basic-authorization-header)