11

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

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

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

I need to know the username.

karel
  • 5,489
  • 46
  • 45
  • 50
Moataz Aahmed Mohammed
  • 1,307
  • 5
  • 22
  • 33
  • possible duplicate of [How to read Windows loged in username with PHP/IIS](http://stackoverflow.com/questions/3899643/how-to-read-windows-loged-in-username-with-php-iis) or perhaps http://stackoverflow.com/questions/12299089/authenticating-via-ldap-for-the-current-windows-users-credentials? – PeeHaa Oct 05 '12 at 21:43
  • 3
    This is not a duplicate. The question is regarding current windows user who runs php-cgi. The answer marked as correct, but actually shows wrong data. Run `` to show current php user. If impersonation is turned on in php.ini `fastcgi.impersonate = 1` then `IUSR` (default IIS User) will be shown by default, otherwise `IIS AppPool\[your website app pool]`. – Artur A Feb 10 '17 at 14:44

1 Answers1

18

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:

<?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.

To see all of the environment variables you can do:

<?php global $_ENV; var_dump($_ENV); ?>
dsas
  • 1,650
  • 18
  • 30
  • 8
    This gave me the current machine name not user whose use this machine now ex if your machine name =python and current logging user name =Moataz it will get python not moataz and i want username moataz to create folders on this desktop, there is any way to do that. – Moataz Aahmed Mohammed Oct 05 '12 at 21:49
  • Thanks in advance your post add to me more knowledge. Thanks – Moataz Aahmed Mohammed Oct 05 '12 at 22:06
  • 4
    Same problem than moataz, the returned string is the computer name, not logged username... – Waza_Be Feb 03 '15 at 10:27
  • 1
    btw. it's the current computer name where the script is running on as far as the computer has one. – Dwza Jul 07 '15 at 11:15
  • Also access to `$_ENV` depends upon the php.ini setting `variables_order = "GPCS"` and `E` is normally turned OFF for obvious security reasons – RiggsFolly Feb 15 '17 at 10:25
  • not sure why this is marked as an answer. If you use php on iis, [this](https://stackoverflow.com/questions/3899643/how-to-read-windows-logged-in-username-with-php-iis/3903024#3903024) should be the answer. also, make sure you are not using anonymous authentication. – Weihui Guo Feb 09 '18 at 12:57