在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:20:08  来源:igfitidea点击:

Find Windows username in PHP

phpwindows

提问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

更多信息



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.

如果您指的是查看网页的人的用户名 - 由于浏览器的安全问题,这是不可能的。