简体   繁体   English

在PHP中找到Windows用户名

[英]Find Windows username in PHP

How can I retrieve the username of the currently logged on Windows user in PHP?如何在 PHP 中检索当前登录的 Windows 用户的用户名?

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.如果您指的是查看网页的人的用户名 - 由于浏览器中的安全问题,这是不可能的。

This code will display username under which PHP/WebServer is run.此代码将显示运行 PHP/WebServer 的用户名。 If you run such script in CLI mode, it will show your login name, otherwise it will be username for the webserver user:如果您在 CLI 模式下运行此类脚本,它将显示您的登录名,否则它将是网络服务器用户的用户名:

$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的更多详细信息,请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM