在 PHP 中查找 Windows 用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6611797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find Windows username in PHP
提问by PJK72
How can I retrieve the username of the currently logged on Windows user in PHP?
如何在 PHP 中检索当前登录的 Windows 用户的用户名?
采纳答案by Gordon
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:
此代码将显示运行 PHP/WebServer 的用户名。如果你在 CLI 模式下运行这样的脚本,它会显示你的登录名,否则它将是 web 服务器用户的用户名:
$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
- 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:
另一种选择是通过 WMIC:
exec('wmic COMPUTERSYSTEM Get UserName', $user)
print_r($user);
For more details on WMIC
, see
有关更多详细信息WMIC
,请参阅
回答by Hyman Murdoch
Assuming you are referring to the username of the user running PHP, this can be retrieved using:
假设您指的是运行 PHP 的用户的用户名,可以使用以下方法检索:
$username = getenv("username");
If you mean the username of the person viewing the webpage - due to security issues in browsers, this is not possible.
如果您指的是查看网页的人的用户名 - 由于浏览器的安全问题,这是不可能的。