Find Windows username in PHP

14,616

Solution 1

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

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

Share:
14,616
PJK72
Author by

PJK72

Updated on August 21, 2022

Comments

  • PJK72
    PJK72 over 1 year

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