How to find (and capture) the user running PHP?

0

I know that the command phpinfo() returns this:

But I want to know if there is any way to capture this information (user / group) in a variable.

    
asked by anonymous 19.01.2017 / 17:23

2 answers

2

I found out. You can do this using:

$uid = posix_getuid();
$userinfo = posix_getpwuid($uid);
print_r($userinfo);

Or:

print posix_getpwuid(posix_geteuid())['name'];

Source: How to check what user php is running as? .

    
19.01.2017 / 17:36
0

You can use $_SERVER['USER'] .

If a person accesses site.com/files.php , $_SERVER['USER'] would be nginx , for example. While this would be executed, via SSH (php /www/files.php) it would be the user name connected to SSH, root for example.

Test this using:

echo $_SERVER['USER'];

You can do for example:

if($_SERVER['USER'] === 'www-data'){
//É acessado via apache
}else{
//Não acessado por apache
}
    
19.01.2017 / 21:21