PHP:get_current_user() 与 exec('whoami') [英] PHP: get_current_user() vs. exec('whoami')
问题描述
问题的简短版本:
有什么区别get_current_user();
和 exec('whoami');
?
问题的长版:
- 我在 Mac 上使用 XAMPP 本地主机.
- 我正在使用 Apache,正在构建一个 PHP基于网站的文件夹(我们称之为 folderxyz)htdocs 文件夹(在某些 Linux+Apache 版本中为 var/www).
- 我在玩数据库连接,测试此处描述的 PDO::ERRMODE_EXCEPTION:链接
- I'm on a XAMPP Localhost on a Mac.
- I'm using Apache, building a PHP based website in a folder (let's call it folderxyz) within the htdocs folder (var/www in some flavors of Linux+Apache).
- I was playing around with a database connection, testing out PDO::ERRMODE_EXCEPTION described here: Link
我收到了这个错误:
file_put_contents(PDOErrors.txt):无法打开流:权限拒绝...
file_put_contents(PDOErrors.txt): failed to open stream: Permission denied...
所以我做了一些调查,似乎要解决这个问题,我需要将文件 PDOErrors.txt 的 CHMOD
设置更改为 777.
So I did some sleuthing around and it seems that to fix this I need to change the CHMOD
settings of file PDOErrors.txt to 777.
然而,我的问题是关于别的东西.在这个过程中我意识到我没有清楚地理解Apache、PHP和MySQL中user
的概念.
However, my questions are about something else. During this process I realized that I don't clearly understand the concept of user
in Apache, PHP, and MySQL.
- PHP 手册说
get_current_user()
获取当前 PHP 脚本所有者的名称" 链接 - PHP 手册说
exec('whoami')
返回 拥有正在运行的 php/httpd 进程的用户名" 链接 - 当我使用
get_current_user()
时,我会得到我的firstnamelastname
,这是我在 Mac 上的帐户名. - 当我使用
exec('whoami')
时,我得到daemon
.
- The PHP manual says that
get_current_user()
"Gets the name of the owner of the current PHP script" Link - The PHP manual says that
exec('whoami')
returns "the username that owns the running php/httpd process" Link - When I use
get_current_user()
, I get myfirstnamelastname
, which is my account name on my Mac. - When I use
exec('whoami')
, I getdaemon
.
所以...
firstnamelastname
和daemon
之间是什么关系?- 当前 PHP 脚本的所有者" 和 拥有正在运行的 php/httpd 进程的用户名" 之间是什么关系?
- 谁需要获得写入 PDOErrors.txt 的权限?是
firstnamelastname
还是daemon
? - 谁需要获得写入 PDOErrors.txt 的权限?是 Apache 还是 PHP(或两者)?
- 类似 Unix 的
root
帐户的概念是否存在于此处?
- What's the relationship between
firstnamelastname
anddaemon
? - What's the relationship between the "the owner of the current PHP script" and "username that owns the running php/httpd process" ?
- Who needs permission to write to PDOErrors.txt? Is it
firstnamelastname
ordaemon
? - Who needs permission to write to PDOErrors.txt? Is it Apache or PHP (or both) ?
- Does the concept of a unix-like
root
account factor-in anywhere here ?
<小时>
我更新了它以反映我不必更改 CHMOD 设置的 folderxyz.我不得不更改文件 PDOErrors.txt
I updated this to reflect that it wasn't the folderxyz that I had to change CHMOD settings for. I had to change the settings for the file PDOErrors.txt
在这里操作:为了将来参考,我在这里提出了一个针对 Linux 平台的并行问题(附带对正在发生的事情的直观解释):https://stackoverflow.com/questions/31389892/why-is-the-output-www-data-in-one-case-and-root-in-another
OP here: for future reference, I put up a parallel question for the Linux platform here (with an accompanying intuitive explanation of what's going on): https://stackoverflow.com/questions/31389892/why-is-the-output-www-data-in-one-case-and-root-in-another
推荐答案
get_current_user()
(应该)返回文件的所有者,在本例中是firstnamelastname
.然而,已经报告了此功能在平台之间不一致的问题.因此,我不会相信它的输出.daemon
是运行 Apache 的用户.- PHP 脚本的所有者是根据操作系统拥有文件本身的用户.您可以在脚本所在的目录中运行
ls -la
以查找文件所属的用户和组. - 无论您编辑脚本的哪个用户都需要能够编写它,因此很可能是
firstnamelastname
(+rw
). - 对于文件夹本身,你应该有
+rx
(执行和读取)daemon
和 PHP 文件,+r
(读).在我安装 XAMMP 时,他们通过将htdocs
中的所有内容设置为公共可读来完成此操作,因此daemon
可以读取它,但不能写入. - Mac 有一个 root 帐户,该帐户通常拥有
htdocs
或www
目录.它填补了传统 unix root 用户的角色.
get_current_user()
(should) return the owner of the file, which isfirstnamelastname
in this case. There have been reported issues that this function is inconsistent between platforms however. As such, I would not trust its output.daemon
is the user Apache is running as.- The owner of the PHP script is the user who owns the file itself according to the operating system. You can run
ls -la
in the directory your scripts are in to find the user and group the file belongs to. - Whichever user you're editing your scripts with needs to be able to write it, so most likely,
firstnamelastname
(+rw
). - For the folder itself, you should have
+rx
(execute and read) fordaemon
and for the PHP file,+r
(read). On my installation of XAMMP, they've done this by setting everything inhtdocs
as public readable, thusdaemon
can read it, but not write to it. - Mac has a root account that typically owns the
htdocs
orwww
directory. It fills the role of a traditional unix root user.
以下是有关文件所有者/组和进程所有者的一些信息:
Here is some information on the file owners/groups and the process owner:
host:~$ ls -l /Applications/XAMPP/xamppfiles/htdocs
drwxr-xr-x 3 root admin 4096 2015-01-01 00:01 .
drwxr-xr-x 3 root admin 4096 2015-01-01 00:01 ..
-rw-r--r-- 1 firstnamelastname admin 189 2015-01-31 20:45 index.php
host:~$ ps aux | grep httpd | head -n1
daemon 45204 0.0 0.1 2510176 10328 ?? S Tue11AM 0:01.38 /Applications/XAMPP/xamppfiles/bin/httpd -k start -E /Applications/XAMPP/xamppfiles/logs/error_log -DSSL -DPHP
如果你想让守护进程用户可以写一个文件,你可以创建一个新文件夹并将其命名为组 admin
的所有者(这样你也可以使用它),并给它 +rwx
用于用户和组,+rx
用于公共:
If you wanted to make a file writeable by the daemon user, you can create a new folder and name it as the owner with the group admin
(so you can use it too), and give it +rwx
for the user and group, with +rx
for public:
host:~$ cd /Applications/XAMPP/xamppfiles/htdocs
host:htdocs$ mkdir some_dir
host:htdocs$ chmod 775 some_dir
这篇关于PHP:get_current_user() 与 exec('whoami')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!