apache 我可以像在 PHP 中一样找到 Web 服务器正在运行的用户吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/827327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:33:40  来源:igfitidea点击:

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

phpapache

提问by Ayman Hourieh

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 编写一个简单的 Web 应用程序,它需要对服务器上的目录具有写访问权限,如果该目录不可写,我想显示一条错误消息,说明将目录的所有者设置为 Web 服务器是谁以(例如 www-data,nobody)的身份运行,但由于大多数人不知道这一点,因此能够告诉他们将目录发给谁会很好。是否可以从 PHP 中找到它?

采纳答案by karim79

Check out shell_exec()

查看shell_exec()

回答by Ayman Hourieh

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

回答by Ben Blank

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.

我使用的快速而肮脏的技巧是将文件写入/tmp/. 它应该使用 PHP 进程使用的用户和组创建。

回答by gnud

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.

文档在这里

回答by Jordan S. Jones

If on linux/unix:

如果在 linux/unix 上:

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

回答by BCS

If you can run some bash, you can use whoamior you might be able to poke around in /proc

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

回答by gnarf

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.

该错误消息在 Windows 服务器环境中分崩离析。“请确保 Web 服务器对目录具有写访问权限”可能是更好的错误消息。

回答by Yuseferi

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

在某些服务器中使用echo exec('whoami');或不 exec('whoami',$out); echo $out;返回任何内容,但在每种情况下您都可以使用get_current_user获取当前用户

<?php
echo get_current_user();
?>