The following will give you the user who is running the PHP process. For example, if you are running Apache, it will give you the user who owns the Apache process. If you run from the command line then you will get the user name of the logged in person.
If you run this through Apache (or any webserver) and that web server is running as user _www on your computer and you are logged in as user zippy, then these will return you _www, not zippy.
On linux or macOS:
$processUser = exec('whoami');
Or:
$userInfo = posix_getpwuid(posix_geteuid());
$processUser = $userInfo['name'];
You could also try to see if it is available through the environment.
$user = $_ENV['USERNAME'] ?? '-unknown';
I used the following Java as an example:
public class Main {
public static void main(String[] args) {
String username = System.getProperty("user.name");
System.out.println("username = " + username);
}
}
Then I used the following PHP:
<?php
echo `whoami`;
echo "\n";
Then I ran on the command line and got the exact same output.
On macOS and Linux, this will give you a list of all users currently logged in:
<?php
$users = exec('/usr/bin/users', $output);
print_r($output);
According to this Oracle Java doc System.getProperty("user.name") gives you "User account name". I am assuming that in a web server environment, this will give you the name of the user that is running the Java or Webserver process.
I am not entirely sure these work on Windows if that's what you are using.