PHP:get_current_user()与EXEC('WHOAMI“) [英] PHP: get_current_user() vs. exec('whoami')
问题描述
短版的问题:的
什么是之间的区别 get_current_user();
和 EXEC('WHOAMI');
龙版的问题:的
- 我在一个XAMPP本地主机Mac上。
- 我使用Apache,构建PHP
文件夹中为基础的网站(我们称之为 folderxyz )内
htdocs文件夹(在Linux中+ Apache的一些味道无功/网络)。 - 我在玩弄一个数据库连接,
测试出PDO ::这里描述ERRMODE_EXCEPTION:<一href=\"http://$c$c.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059\"相对=nofollow>链接
和我得到这个错误:
的file_put_contents( PDOErrors.txt ):未能打开流:权限
否认...
块引用>所以,我做了一些侦探周围,似乎解决这个问题我需要改变文件的
CHMOD
设置的 PDOErrors.txt 以777不过,我的问题是关于别的东西。在这个过程中,我意识到,我没有了解清楚
用户
在Apache中的概念,PHP和MySQL。
- PHP手册说,
get_current_user()
的获取当前PHP脚本所有者的名义的的链接- PHP手册说,
EXEC('WHOAMI')
返回拥有的运行PHP / httpd进程的用户名的的链接- 当我使用
get_current_user()
,我让我的名姓 code>,这是我的帐户名称在我的Mac。
- 当我使用
EXEC('WHOAMI')
,我得到后台
。所以...
- 有什么关系
名姓 code>和
后台
?- 什么是之间的关系的当前PHP脚本的所有者的和的用户名拥有该运行PHP / httpd进程的?
- 谁需要写权限的 PDOErrors.txt ?它是
名姓 code>或
后台
?- 谁需要写权限的 PDOErrors.txt ?它是Apache或PHP(或两者)?
- 是否类似Unix的
根的概念
帐户因子中的任何地方吗?编辑:我更新了这个以反映它不是在 folderxyz ,我不得不改变CHMOD设置。我不得不改变设置文件的 PDOErrors.txt
OP此处以供将来参考,我提出了一个问题,同时为Linux平台在这里(有什么事情的伴随直观的解释):<一href=\"http://stackoverflow.com/questions/31389892/why-is-the-output-www-data-in-one-case-and-root-in-another\">Why是输出`WWW-data`在一种情况下和`root`在另一个?
解决方案
get_current_user()
(应该)返回文件的所有者,这是名姓 code>在这种情况下。曾有报告的问题,这个函数然而平台之间的不一致。因此,我不相信它的输出。
后台
是Apache是运行作为用户。- PHP脚本的所有者是谁拥有根据操作系统文件本身的用户。您可以运行
LS -la
目录中的脚本是找到该文件所属的用户和组。- 用户无论你使用的需要编辑的脚本,以便能够最有可能写出来,所以,
名姓 code>(
+ RW
)。- 有关文件夹本身,你应该有
+ RX
为守护程序(执行和读取)
并为PHP文件,+ R
(读取)。在我安装XAMMP的,他们已经被公众可读,从而守护在
可以读它,但不能写入。的htdocs
设置都做到了这一点- Mac有,通常拥有
的htdocs
或WWW
目录中的root帐户。它填补了传统的UNIX root用户的作用。下面是关于文件所有者/组的一些信息和流程所有者:
主持人:〜$ ls -l命令/应用/ XAMPP / xamppfiles / htdocs目录
drwxr-XR-×3根admin 4096 2015年1月1日0时01分。
drwxr-XR-×3根admin 4096 2015年1月1日00:01 ..
-rw-R - R-- 1名姓管理189 2015年1月31日20:45的index.php主持人:〜$的ps aux | grep的的httpd |头-n1
守护进程45204 0.0 0.1 2510176 10328?小号Tue11AM 0:01.38 /应用/ XAMPP / xamppfiles /斌/的httpd -k开始-E /应用/ XAMPP / xamppfiles /日志/ error_log中-DSSL -DPHP如果你想使这个守护进程用户的文件写的,你可以创建一个新的文件夹并将其命名为同组的所有者
管理
(所以你可以使用它太),并给它+ RWX
为用户和组,以+ RX
公开:主持人:〜$ CD /应用/ XAMPP / xamppfiles / htdocs目录
主持人:htdocs目录$的mkdir some_dir
主持人:htdocs目录$ CHMOD 775 some_dirShort version of the question:
What's the difference between
get_current_user();
andexec('whoami');
?Long version of the question:
- 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
And I got this error:
file_put_contents(PDOErrors.txt): failed to open stream: Permission denied...
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.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.
- 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
.So...
- 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 ?
Edit: 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
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): Why is the output `www-data` in one case and `root` in another?
解决方案
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
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屋!