Re: How to get server user?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 02/17/2015 12:22 AM, Jeffry Killen wrote:

On Feb 16, 2015, at 7:16 PM, Jason Edgecombe wrote:

On 02/16/2015 08:59 PM, Jeffry Killen wrote:
I've been looking over the output from phpinfo to find a way
to portably get the server user name. It is not obvious to me...

Can I get that from an $_ENV or $_SERVER value?

I have a web base cms system with a file editor and
want to test for ownership of the files and directories
without having to hard code the server name.

I can get file owner ship but if I want to verify it is
the server user account, I would do:
(pseudo code)
$_name = getOwner()
if($_name == < portable reference to server user name>)
 {
 // proceed
}

This would be for enhanced access permissions analysis.
If a file is being written to by the server user (via php code)
it would be helpful to determine that to forstall error message
generation.

Thanks
JK

I suggest just writing to the file and catching the error exception. There are multiple file systems with various permission schemes. One examples include is Linux ext4 with POSIX ACL's. Another example is OpenAFS where special ACL's are used while the 'group' and 'other' unix mode bits are mostly ignored. Selinux is another ACL scheme that might be effect. I've had multiple user non-web non-PHP applications 'fail' to write files because it detected that the mode bits were incorrect when the write() syscalls would have succeeded.

Please save yourself and your sysadmin some grief by simplying the code to attempt the operation and let the OS and file system verify the access.

Sincerely,
Jason
(Just another Linux admin)

Thanks for the info. So, in short, I should just hard code it?
Thanks for time and attention
JK


I'm saying proceed, then check the result. For example, you might use this code if you want to write to a new file:
|<?php
$file = 'people.txt';
// The new person to add to the file
$person = "John Smith\n";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
if (file_put_contents($file, $person, LOCK_EX)) {
  echo "success\n";
} else {|
||  echo "failed\n";
|}
?>|

If you are replacing the contents of a file, it's usually better to copy/write to a new file, update the file, then rename() the new file to the old file's name. This is the same as 'mv' in Linux/unix, which makes things a little cleaner. I think that rename/mv is atomic, but I could be wrong.

Jason

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux