2

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.

jami
  • 190
  • 2
  • 14
  • Are you using PHP through an Apache module or via CGI, FastCGI, PHP-FPM... etc ? – SirDarius Nov 29 '13 at 15:22
  • http://www.php.net/manual/en/reserved.variables.environment.php#98113 - read that, and if your ENV is enabled in INI, then include information in your question that SirDarius asked. – N.B. Nov 29 '13 at 15:23

2 Answers2

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)

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • The $_SERVER is the only solution that works fine for me :) Like i said in the reply to drull, the 'E' in variables_order only work in combination with allow globals. But that would be the gate to (security) hell – jami Dec 02 '13 at 14:13
  • :) ... if you once find out what caused the problem it would be nice if you could add that information here – hek2mgl Dec 02 '13 at 14:25
  • Oh yes you are right. I left my variables_order to "GPCS" but i use $_SYSTEM to retrieve the variables instead of $_ENV. This works fine for me. – jami Dec 02 '13 at 15:28
  • ok, should work fine (unless you are using environment variable names that are already part of `$_SERVER` – hek2mgl Dec 02 '13 at 15:56
1

Set

variables_order = "EGPCS"

in your php.ini

drull
  • 481
  • 1
  • 3
  • 9
  • Indeed my variables_order is "GPCS". But adding the E does not work. Because, if i read the doc correctly, you have to set register_globals to on. This is not recommened by serveral sources. http://www.php.net/manual/en/security.globals.php – jami Dec 02 '13 at 13:43