我创建了php程序,试图在其中获取当前windows用户名。我在服务器上安装了这个,服务器上有多个用户,例如:[(administrator--user-1),(sam--user-2),(mac--user-3)]等等
我使用user-1启动我的example。我在localhost(localhost/myapplication/index.php)中启动我的应用程序我得到--->管理员的正确o/p
echo getenv("username"); or echo get_current_user(); or echo trim(shell_exec('whoami'));
对于user-2(他登录到他的windows会话)没有必要启动examplep,因为它已经由user-1启动了user-2在localhost(localhost/myapplication/index.php)中启动应用程序(使用不同的登录会话)
当我执行时
echo getenv("username"); or echo get_current_user(); or echo trim(shell_exec('whoami'));
我得到o/p-->管理员而不是sam
你能帮我吗?
由于我在上面发布的原因,不可能按您的方式行事。然而,也可以采取其他方式。
我创建了一个C脚本
#include <stdlib.h>
int main(int argc, char *argv[])
{
char *p=getenv("USER");
if(p==NULL) return EXIT_FAILURE;
printf("%s'n",p);
return 0;
}
编译此代码。如果你不知道怎么做,可以在谷歌上搜索How to compile C code
编译完成后,将编译代码保存在与php脚本相同的目录中。我把我的保存为a.out
然后在php代码中执行以下操作:
exec("./a.out");
这将返回当前登录的用户。