Find Windows username in PHP
14,616
Solution 1
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
Solution 2
Assuming you are referring to the username of the user running PHP, this can be retrieved using:
$username = getenv("username");
If you mean the username of the person viewing the webpage - due to security issues in browsers, this is not possible.
Author by
PJK72
Updated on August 21, 2022Comments
-
PJK72 over 1 year
How can I retrieve the username of the currently logged on Windows user in PHP?