6.13. Accessing the Authenticated Username
Problem
You want to know the name of the user who has authenticated.
Solution
Some scripting modules, such as mod_php, provide a standard interface for
accessing values set by the server. For instance, to obtain the
username that was used to authenticate from within a PHP script, it
would access a field in the $_SERVER superglobal array:
$auth_user = $_SERVER['REMOTE_USER'];
For a Perl or mod_perl script, use:
my $username = $ENV{REMOTE_USER};In a Server-Side Include (SSI) directive, this may look like:
Hello, user <!--#echo var="REMOTE_USER" -->. Thanks for visiting.
Other scripting modules may provide specific means of accessing
this information. For those that don’t, or for nonscript applications,
use the appropriate means to consult the REMOTE_USER environment variable.
Discussion
When a user has authenticated, the environment variable REMOTE_USER is set to the name with which
she authenticated. You can access this variable in CGI programs, SSI
directives, PHP files, and a
variety of other methods. The value also will appear in your access_log file.
Note that although it is the convention for an authentication module to set this variable, there are reportedly some third-party authentication modules that do not set it but provide other methods for accessing that information.
See Also
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access