Sentence
A bit late, but although the following is workaround, it solves the requirement as it works very well:
<? function get_sys_usr() { $unique_name = uniqid(); // not-so-unique id $native_path = "./temp/$unique_name.php"; $public_path = "http://example.com/temp/$unique_name.php"; $php_content = "<? echo get_current_user(); ?>"; $process_usr = "apache"; // fall-back if (is_readable("./temp") && is_writable("./temp")) { file_put_contents($native_path,$php_content); $process_usr = trim(file_get_contents($public_path)); unlink($native_path); } return $process_usr; } echo get_sys_usr(); // www-data ?>
Description
The selection of the code above is inaccurate, copy and paste it into your favorite editor and view it as PHP code, or save and test it yourself.
As you probably know, get_current_user() returns the owner of the “current script run”, so if you are not “chown” a script on the server for the web server user, it will most likely be “nobody”, or if the developer the user exists in the same OS, he will rather display this username.
To get around this, we create a file with the current start of the process. If you just require() in the current script run, it will return the same as parent-script, as indicated; therefore, we need to run it as a separate request for entry into force.
Flow process
To make this effective, consider launching a design pattern that includes a “run mode”, so when the server is in “development or test mode”, then only it can run this function and save its output somewhere in include, or just text or a database, or depending on which one.
Of course, you can change some of the features of the code above, since you want to make it more dynamic, but the logic looks like this:
- define a unique link to limit interference to other users
- determine the local file path for writing a temporary file
- define a common url / path to run this file in your own process
- write a temporary php file that displays the name of the owner of the script
- get the result of this script by making a request to it
- delete the file because it is no longer needed - or leave it if you want
- returns the result of the query as the return value of a function
user4244405 May 26 '17 at 10:37 2017-05-26 10:37
source share