1

I'm wanting to log certain events to a file. Here is the relevant PHP code:

$logFile = '/var/logs/private/do_not_backup/lms2.0_test_errors.log';
$logMessage = 'New Common Quiz Review about to be created with userQuizId of ' . $userQuizId . '\n\n';

$filePutResult = file_put_contents($logFile, $logMessage, FILE_APPEND);

if (!$filePutResult){
  $currentUser = get_current_user();
  die ($currentUser . ' could not append ' . $logMessage . ' to log file ' . $logFile . '!');
}

A sample die message would be:

root could not append 'New Common Quiz Review about to be created with userQuizId of 7316553\n\n' to log file /var/logs/private/do_not_backup/lms2.0_test_errors.log!

Here is the ls -l from the log file location:

[userid]@ip-[ip address]:/var/logs/private/do_not_backup$ ls -l
-rw-r--r-- 1 root     www-data          0 Nov 22 20:43 lms2.0_test_errors.log

The file is empty as I just created it and am yet to get a successful write to it. Following a die output, I changed the owner from www-data to root, but I left the group as-is.

The PHP page that is quoted-in-part above resides in /var/www/vhosts/[domain]/httpdocs/lms/assessment_review.php on the same server as the target log file. The server itself is running nginx 1.18.0 with PHP 7.4.3 on Ubuntu Linux 20.04.3.

Neil Wehneman
  • 154
  • 1
  • 12
  • 1
    According to [the docs](https://www.php.net/manual/en/function.get-current-user.php), that function returns the owner of the file itself, not the process that is executing PHP, check the first two comments – Chris Haas Nov 23 '21 at 05:06
  • Nice Q&A! See also: [How to check what user php is running as?](https://stackoverflow.com/q/7771586/1729441) – August Janse Oct 21 '22 at 11:58

1 Answers1

1

As Chris Haas pointed out, get_current_user does not return what I thought it was going to return. As the first comment he pointed towards notes, code akin to the following is necessary to get the appropriate userid:

$processUser = posix_getpwuid(posix_geteuid());
print $processUser['name'];

That showed me that www-data is running the process, and once I made them owner of the log file I got the logging I expected.

Neil Wehneman
  • 154
  • 1
  • 12