Php get current apache user

I'm on a foreign linux system and need to determine the user that apache runs on (and so does php).

The aim: I need to get the owner of the script (this is no problem as I can use SplFileInfo) and compare it to the owner of the apache process.

I'm open to any alternative proposals.

Regards, Mario

Edit: Additional info:

The script is a thumbnail generator, that uses an XML file to generate thumbs from larger images. The script needs to create folders and write files. As I cannot influence the php configuration and I do not have any shell access, this has to be done very silently. The creation process stopps via exception and sends a mail on failue. As most of php's function cannot throw exceptions on failue, I need some manual checks to determine the environment I'm in. Therefore I need the apache user to compare it to some directory or fileowner.

asked Apr 30, 2009 at 8:22

Mario MuellerMario Mueller

1,4102 gold badges13 silver badges16 bronze badges

2

You can call the php exec function to execute whoami:

<?php echo exec('whoami'); ?>

answered Apr 30, 2009 at 8:39

grantcgrantc

1,70312 silver badges13 bronze badges

5

see posix_getuid() and posix_getpwuid()

answered Apr 30, 2009 at 8:41

AnonymousAnonymous

2,9114 gold badges23 silver badges23 bronze badges

Some complicated answers here.

This works for me:

$user = getenv('APACHE_RUN_USER');

Not sure if this is just a new thing that been added to apache since this question was asked but it's definitely there now.

answered Jul 1, 2013 at 23:10

Jamie CarlJamie Carl

1,19613 silver badges21 bronze badges

1

phpinfo will dump a lot of system information. For apache2 installs, there is a section that displays the apache user and group ids. Try creating a php script that just has one line, a call to phpinfo(), and open it in your web browser.

answered Apr 30, 2009 at 8:30

Php get current apache user

zombatzombat

91k24 gold badges155 silver badges164 bronze badges

2

Some php script must be run on apache user (cli), whoami is not appropriate in that case. Here is my solution :

$output = exec('apachectl -S 2>/dev/null | grep User');
$apacheUser = preg_match('/name="([^"]+)"/', $output, $match) ? $match[1] : 'www-data';

answered Nov 14, 2018 at 8:37

atmacolaatmacola

3252 silver badges8 bronze badges

(PHP 4, PHP 5, PHP 7, PHP 8)

get_current_userGets the name of the owner of the current PHP script

Description

get_current_user(): string

Returns the name of the owner of the current PHP script.

Parameters

This function has no parameters.

Return Values

Returns the username as a string.

Examples

Example #1 get_current_user() example

<?php
echo 'Current script owner: ' get_current_user();
?>

The above example will output something similar to:

Current script owner: SYSTEM

See Also

  • getmyuid() - Gets PHP script owner's UID
  • getmygid() - Get PHP script owner's GID
  • getmypid() - Gets PHP's process ID
  • getmyinode() - Gets the inode of the current script
  • getlastmod() - Gets time of last page modification

justin samuel

16 years ago

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'];
?>

south dot bucks at gmail dot com

10 years ago

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

s dot bond1 at lse dot ac dot uk

15 years ago

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.

chris at ocproducts dot com

2 years ago

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.

nick at little-apps dot org

9 years ago

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'));
?>

login dot naitsirch at arcor dot de

7 years ago

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
?>

How do I find apache username?

Linux: Find Out Apache User Name.
apache2 (1st column) – Apache service / server name..
4122 (2nd column) – Apache server PID..
www-data (3rd column) – Apache server username for PID. This gives you apache username..

What user is PHP?

1 Answer. The user that runs apache/php is www-data .

How do I find the web server group?

To find the web server user's group:.
CentOS: grep -E -i '^user|^group' /etc/httpd/conf/httpd.conf. Copy. or. grep -Ei '^user|^group' /etc/httpd/conf/httpd.conf. Copy. ... .
Ubuntu: ps aux | grep apache to find the apache user, then groups <apache user> to find the group. Typically, the username and the group name are both www-data..