简体   繁体   English

我可以在PHP中找到运行Web服务器的用户吗?

[英]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. 我正在用PHP编写一个简单的Web应用程序,需要对服务器上的目录具有写入权限,如果目录不可写我想显示一条错误消息,说明将目录的所有者设置为webserver是谁正在运行(例如,www-data,nobody),但由于大多数人不知道这一点,能够告诉他们将目录列入谁的状态会很好。 Is it possible to find this from PHP? 是否可以从PHP中找到它?

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

The quick-and-dirty trick I use is to write a file to /tmp/ . 我使用的快速和肮脏的技巧是将文件写入/tmp/ It should be created with the user and group used by the PHP process. 它应该使用PHP进程使用的用户和组创建。

On Unix platforms, this solution might work even with safe mode on, provided that the posix extension is installed or compiled in. 在Unix平台上,只要安装或编译了posix扩展,这个解决方案即使在安全模式下也可以工作。

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

Docs are here . 文件在这里

If on linux/unix: 如果在linux / unix上:

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

In some servers using echo exec('whoami'); 在一些使用echo exec('whoami');服务器中echo exec('whoami'); or exec('whoami',$out); echo $out; 或者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 什么都不返回,但在每种情况下,您都可以使用get_current_user获取当前用户

<?php
echo get_current_user();
?>

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

That error message falls apart in a windows server environment. 该错误消息在Windows服务器环境中崩溃。 "Please make sure the web server has write access to the directory" is probably a better error message. “请确保Web服务器具有对目录的写访问权限”可能是更好的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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