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
PHP Get All Subdirectories of a Given Directory
How to Properly Use a Pdo Object For a Parameterized Select Query
How to Overwrite a Function in PHP
Blocking Comment Spam Without Using Captcha
Incorrect Integer (2147483647) Is Inserted into MySQL
Disable PHP in Directory (Including All Sub-Directories) With .Htaccess
How to Only Allow Certain Filetypes on Upload in PHP
Facebook Graph API, How to Get Users Email
What Is the Best Collation to Use For MySQL With PHP
PHP $_Server['Http_Host'] Vs. $_Server['Server_Name'], Am I Understanding the Man Pages Correctly
Include PHP Inside JavaScript (.Js) Files
How to Prevent Code Injection Attacks in PHP
Error Message Strict Standards: Non-Static Method Should Not Be Called Statically in PHP
How to PHP-Unserialize a Jquery-Serialized Form