Can You Get a Windows (Ad) Username in PHP

Can you get a Windows (AD) username in PHP?

Check the AUTH_USER request variable. This will be empty if your web app allows anonymous access, but if your server's using basic or Windows integrated authentication, it will contain the username of the authenticated user.

In an Active Directory domain, if your clients are running Internet Explorer and your web server/filesystem permissions are configured properly, IE will silently submit their domain credentials to your server and AUTH_USER will be MYDOMAIN\user.name without the users having to explicitly log in to your web app.

Get Windows username of current user using php?

If by current windows user you mean the user running the script then that is set in an environment variable which you can get using:

<?php echo getenv("username"); ?>

If you want to get the home directory of the user running the script you should use

<?php echo getenv("HOMEDRIVE") . getenv("HOMEPATH"); ?>

This should output either C:\Users\Fred or C:\Documents and Settings\Fred depending on if you are using windows Vista/7 or windows XP.

To see all of the environment variables you can do:

<?php global $_ENV; var_dump($_ENV); ?>

Find Windows username in PHP

This code will display username under which PHP/WebServer is run. If you run such script in CLI mode, it will show your login name, otherwise it will be username for the webserver user:

$obj = new COM("wscript.network");
echo $obj->username;

Further information

  • http://php.net/manual/en/function.com-get.php
  • http://msdn.microsoft.com/en-us/library/3fxhka75%28v=vs.85%29.aspx

An Alternative would be via WMIC:

exec('wmic COMPUTERSYSTEM Get UserName', $user)
print_r($user);

For more details on WMIC, see

  • WMIC - Take Command-line Control over WMI and
  • Windows Management Instrumentation Command

Detect windows domain username in PHP

If you mean Active Directory authentication, and you're running Internet Explorer on the client end, this seems to be possible in some circumstances. See here.



Related Topics



Leave a reply



Submit