Environment is not passed to process opened by proc_open

China☆狼群 提交于 2019-12-10 23:58:28

问题


I have a problem passing environment variables to processes that i opened with proc_open. I found the following example on http://us2.php.net/manual/en/function.proc-open.php

<?php
$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
);

$cwd = '/tmp';
$env = array('some_option' => 'aeiou');

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

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to /tmp/error-output.txt

    fwrite($pipes[0], '<?php print_r($_ENV); ?>');
    fclose($pipes[0]);

    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);

    echo "command returned $return_value\n";
}
?>

The example should echo the env array like the documentation say. But on my machine (PHP 5.4.6-1ubuntu1.4 (cli)) the echoed array is empty. Are there some Suhosin or php.ini restrictions that ban env var passing to processes? I have no idea.


回答1:


If $_ENV is empty, you should have a look at your variables_order ini setting and ensure that the value contains the E

However, you can use $_SERVER instead:

fwrite($pipes[0], '<?php print_r($_SERVER); ?>');

It will contain environment variables too and should be enabled at 99.999% of servers (I guess)




回答2:


Set

variables_order = "EGPCS"

in your php.ini



来源:https://stackoverflow.com/questions/20288822/environment-is-not-passed-to-process-opened-by-proc-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!