环境未传递给 proc_open 打开的进程 [英] Environment is not passed to process opened by proc_open
问题描述
我在将环境变量传递给我用 proc_open 打开的进程时遇到问题.我在 http://us2.php.net/manual 上找到了以下示例/en/function.proc-open.php
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";
}
?>
该示例应该像文档所说的那样回显 env 数组.但是在我的机器上(PHP 5.4.6-1ubuntu1.4 (cli)),回显的数组是空的.是否有一些 Suhosin 或 php.ini 限制禁止将 env var 传递给进程?我不知道.
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.
推荐答案
如果 $_ENV
为空,你应该看看你的 variables_order
ini 设置并确保该值包含 E代码>
If $_ENV
is empty, you should have a look at your variables_order
ini setting and ensure that the value contains the E
但是,您可以使用 $_SERVER
代替:
However, you can use $_SERVER
instead:
fwrite($pipes[0], '<?php print_r($_SERVER); ?>');
它也会包含环境变量,应该在 99.999%
的服务器上启用(我猜)
It will contain environment variables too and should be enabled at 99.999%
of servers (I guess)
这篇关于环境未传递给 proc_open 打开的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!