Short version of the question:
What's the difference between
get_current_user();
and exec('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): https://stackoverflow.com/questions/31389892/why-is-the-output-www-data-in-one-case-and-root-in-another
Update: That question was deleted as it was deemed to be a duplicate of this one (although that was for the Linux platform, and instead of daemon
I was getting www-data
after doing echo exec('whoami');
).
This is what I learned from asking this question in the Linux forum:
the running process is different than the script. Yes, the process emerges from the script, but it's not the same thing. The script is owned by root, but the process is taken over by Apache and is run as www-data.
Overall conclusion: What I learned from this process is that user is supposed to be the Apache daemon (Mac localhost) or www-data (Linux on an internet server) and that I should be using exec('whoami')
to determine this, and I shouldn't care too much about get_current_user()
(which probably should have been named get_current_owner()
).