php 使用php获取当前用户的Windows用户名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12754437/
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-08-25 04:09:34  来源:igfitidea点击:

Get Windows username of current user using php?

phpusernamewindows-users

提问by Moataz Aahmed Mohammed

Possible Duplicate:
How to read Windows loged in username with PHP/IIS

可能的重复:
如何使用 PHP/IIS 读取登录用户名的 Windows

I'm work on php tool that generate Data Access layer and generate some folders and files to user but i need to know how can i get username for current windows user to generate these folders on desktop

我正在使用 php 工具生成数据访问层并向用户生成一些文件夹和文件,但我需要知道如何获取当前 Windows 用户的用户名以在桌面上生成这些文件夹

ex:
C:\users\<username>\desktop

I need to know the username. Thanks

我需要知道用户名。谢谢

回答by dsas

If by current windows user you mean the user running the script then that is set in an environment variable which you can get using:

如果当前的 Windows 用户是指运行脚本的用户,那么它会在您可以使用的环境变量中设置:

<?php echo getenv("username"); ?>

If you want to get the home directory of the user running the script you should use

如果您想获取运行脚本的用户的主目录,您应该使用

<?php echo getenv("HOMEDRIVE") . getenv("HOMEPATH"); ?>

This should output either C:\Users\Fred or C:\Documents and Settings\Fred depending on if you are using windows Vista/7 or windows XP.

这应该输出 C:\Users\Fred 或 C:\Documents and Settings\Fred,具体取决于您使用的是 Windows Vista/7 还是 Windows XP。

To see all of the environment variables you can do:

要查看所有环境变量,您可以执行以下操作:

<?php global $_ENV; var_dump($_ENV); ?>