2

How can I retrieve the username of the currently logged on Windows user in PHP?

user229044
  • 232,980
  • 40
  • 330
  • 338
PJK72
  • 23
  • 1
  • 1
  • 4

2 Answers2

2

Assuming you are referring to the username of the user running PHP, this can be retrieved using:

$username = getenv("username");

If you mean the username of the person viewing the webpage - due to security issues in browsers, this is not possible.

Jack Murdoch
  • 2,864
  • 1
  • 18
  • 27
  • I sure hope that's not possible. – Michael Mior Jul 07 '11 at 14:07
  • Most definitely not possible. Well, you can in IE using ActiveX (if security settings are right, and permissions are given), but otherwise its not happening. – Jack Murdoch Jul 07 '11 at 14:09
  • You can get many other informations, though, they are stored in `$_SERVER`. – SteeveDroz Jul 07 '11 at 14:16
  • Yes, I can Understand, but why with Windows Tools (ASP, SQLServer Sharpoint Ect. Ect ) it's possible? it's strange, no ? – PJK72 Jul 07 '11 at 14:19
  • 1
    Even with Windows things, you won't be able to retrieve the username of the person accessing the website itself. This is not something the browser will ever allow you to access. It doesn't matter what's going on behind the scenes. – Jack Murdoch Jul 07 '11 at 14:20
  • On IIS 7 this is the same as `$_SERVER["USERNAME"]` and it shows me not the username, but computername with $ at the end (like `MYPC$`). If such code run in CLI mode (within IDE, for example), it shows user name indeed. – LazyOne Jul 07 '11 at 15:34
2

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:

$obj = new COM("wscript.network");
echo $obj->username;

Further information


An Alternative would be via WMIC:

exec('wmic COMPUTERSYSTEM Get UserName', $user)
print_r($user);

For more details on WMIC, see

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Nice,but he gave me just "SYSTEM" as word Do you know why? – PJK72 Jul 07 '11 at 14:42
  • 1
    This code will display username under which PHP/WebServer is run. If you run such script in CLI mode, it will show you your login, otherwise it will be username which Apapche/IIS app pool is run under. – LazyOne Jul 07 '11 at 15:31
  • Yes infact I replace username with Computername and He give me the Server Name, means the script is performed at server level, I thinks should be necessary run it at client server – PJK72 Jul 07 '11 at 15:46
  • I try this code but doesn't work :( Do you see something strange ? – PJK72 Jul 07 '11 at 15:55
  • @LazyOne I try this code but doesn't work :( '' Do you see something strange ? – PJK72 Jul 07 '11 at 16:09
  • @PJK72 That's Javascript not PHP. – LazyOne Jul 07 '11 at 16:23
  • @LazyOne Yes I know, but as sayd you before with PHP he give me the "SYSTEM" word and not <> – PJK72 Jul 07 '11 at 18:01
  • @PJK72 What I meant to say there -- is that I'm not that good with JavaScript (just some pretty basic jQuery usage) and cannot really advice here. In any case -- that code may only work in IE if at all. – LazyOne Jul 07 '11 at 18:06
  • @LazyOne Very Clear, I know that with JQuery Library I shoud find the solution, but I don't want use it, for not create too much lines in the code only for recovery the username thanks a lot. – PJK72 Jul 07 '11 at 18:14
  • @LazyOne OK, now it's clear, using a Script code, need to change the IE security to LOW, that's all – PJK72 Jul 08 '11 at 14:25