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

Asked

Viewed 980 times

1

I know the command phpinfo() return that:

inserir a descrição da imagem aqui

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

2 answers

3


I figured it out. You can do it 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?.

1

You can use the $_SERVER['USER'].

If a person accessed site.com/.php file, the $_SERVER['USER'] would be the nginx, for example. While running, via SSH (php /www/.php file) it would be the respective name of the user 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
}
  • I think that Apache does not return $_SERVER['USER']. Mine doesn’t come back. I’m not sure, but I believe www-data is not an exclusive user of Apache. Anyway, $_SERVER will not return what I need because it has script references, and not service.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.