Question

I am trying to run processes using proc_open() function. As specified on the page - I supplied the custom environment variables and tried to print out. It shows all of my supplied variables + always 3 variables : 'SHLVL', 'PWD', '_='. I would like to print/use only my supplied environment variables. Are these 3 always present with this function? Is there any way to have only provided variables? This is all under Linux and PHP5.

//Here is the code to clarify : 
$descriptorspec = array(
0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);

$env = array('MY_VAR' => 'FOO');

$process = proc_open('./run.php', $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {

fwrite($pipes[0], escapeshellcmd($args));
fclose($pipes[0]);

$output = "";
while (!feof($pipes[1])) {
    $output .= fgets($pipes[1]);
}

print "Output is $output \n";
    fclose($pipes[1]);
    $return_value = proc_close($process);

}

Thanks.

Was it helpful?

Solution

You could namespace your environment variables, e.g. PHP_MYVAR instead of MYVAR. This way you can filter based on the common prefix PHP_.

OTHER TIPS

Those three variables are created by the shell. If you don't open a shell, they won't be created.

It was just related to Linux. It works as it supposed to under Solaris. I added regex filter to remove those extra variables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top