简体   繁体   中英

Can I find which user the web server is being run as in PHP?

I'm writing a simple web app in PHP that needs to have write access to a directory on the server, if the directory isn't writable I want to display an error message explaining to set the owner of the directory to whoever the webserver is being run as (eg. www-data, nobody) but as most people don't know this it would be nice to be able to tell them who to chown the directory to. Is it possible to find this from PHP?

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

The quick-and-dirty trick I use is to write a file to /tmp/ . It should be created with the user and group used by the PHP process.

On Unix platforms, this solution might work even with safe mode on, provided that the posix extension is installed or compiled in.

$user = posix_getpwuid(posix_geteuid());
echo $user['name'];

Docs are here .

If on linux/unix:

<?php
$temp = tmpfile();
print_r(posix_getpwuid(fileowner($temp)));
?>

In some servers using echo exec('whoami'); or exec('whoami',$out); echo $out; exec('whoami',$out); echo $out; returns nothing but in every situation you can get the current user with get_current_user

<?php
echo get_current_user();
?>

如果你可以运行一些bash,你可以使用whoami或者你可以在/proc

That error message falls apart in a windows server environment. "Please make sure the web server has write access to the directory" is probably a better error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM