to get the username of the process owner (rather than the file owner), you can use:
<?php
$processUser = posix_getpwuid(posix_geteuid());
print $processUser['name'];
?>
(PHP 4, PHP 5, PHP 7, PHP 8)
get_current_user — 获取当前 PHP 脚本所有者名称
此函数没有参数。
以字符串返回用户名。
示例 #1 get_current_user() 示例
<?php
echo 'Current script owner: ' . get_current_user();
?>
以上示例的输出类似于:
Current script owner: SYSTEM
to get the username of the process owner (rather than the file owner), you can use:
<?php
$processUser = posix_getpwuid(posix_geteuid());
print $processUser['name'];
?>
On Centos, the Red Hat linux clone, this instruction gives the file's OWNER (the first parameter in instruction 'chown'). It does not reveal the file's GROUP.
get_current_user() does NOT reveal the current process' user's identity.
See: posix_getuid() - Return the real user ID of the current process
The information returned by get_current_user() seems to depend on the platform.
Using PHP 5.1.1 running as CGI with IIS 5.0 on Windows NT, get_current_user() returns the owner of the process running the script, *not* the owner of the script itself.
It's easy to test - create a file containing:
<?php
echo get_current_user();
?>
Then access it through the browser. I get: IUSR_MACHINE, the Internet Guest Account on Windows, which is certainly not the owner of the script.
Further testing of behaviour on Windows vs Linux...
On Linux this function is indeed returning the owner of the script. If you want to know the username PHP is running as you can use POSIX functions (or shell_exec with 'whoami').
On Windows this function is returning the username PHP is running as. Both for IIS (IUSR) and Apache (SYSTEM - which comes from the fact Apache is a service on Windows).
The behaviour on Windows is actually useful given that POSIX functions aren't available. If you need to find the owner of the script on Windows perhaps the best way is to shell_exec to use dir /Q, and parse that.
Since this only returns the file owner and not the actual user running the script, an alternative in Linux is:
<?php
$current_user = trim(shell_exec('whoami'));
?>
If you want to get the name of the user who executes the current PHP script, you can use
<?php
$username = getenv('USERNAME') ?: getenv('USER');
echo $username; // e.g. root or www-data
?>
If you have userdir enabled, get_current_user() returns the username of the user hosting the public_html. For example, http://example.com/~bobevans/somescript.php will return bobevans when calling get_current_user().