Edit report at https://bugs.php.net/bug.php?id=63008&edit=1
ID: 63008 User updated by: phpbugs at addiks dot de Reported by: phpbugs at addiks dot de Summary: Need reliable way of listing environment variables. Status: Open Type: Feature/Change Request Package: *General Issues Operating System: Ubuntu/Debian PHP Version: 5.4.6 Block user comment: N Private report: N New Comment: Problem with shell_exec is that it only works as long as program-execution is allowed, so this can also not really be considered reliable. Some people disable program-execution on productive servers for security reason, and prompt them to allow program-execution just to read environment-vars does not sound like the way to go. Reading the environment-variables from phpinfo would be a possible way i have not considered yet. That might work for now, but it sounds more like a dirty workaround. I dont think that phpinfo() was designed to fetch the environment-variables to your application on a regular basis. Who can guarantee that there will always be a phpinfo providing env-var's and that the way it does never change? (I dont like writing software with expiration-date.) My opinion that there should be a 'listenv' like function to reliable list environment-variables has not changed. Previous Comments: ------------------------------------------------------------------------ [2012-09-04 09:04:13] ras...@php.net For your use-case it doesn't sound like performance is an issue so you could just do $env = shell_exec("env"); It is also technically available via phpinfo(INFO_ENVIRONMENT) although that outputs it with html tags, so you would have to ob-parse it. ------------------------------------------------------------------------ [2012-09-04 08:32:21] phpbugs at addiks dot de There _would_ be also a third way of accessing environment-variables, accessing '/proc/self/environ'. But that is not possible on most systems because when using PHP as apache2-module the process gets spawned as root and setuid'd to the apache-user, which leads to non-readable '/proc/self/*' files. Maybe there can be something done in that direction? ------------------------------------------------------------------------ [2012-09-04 08:17:46] phpbugs at addiks dot de Description: ------------ Hello there, Currently there are only two ways of accessing environment-variables: a) Using the getenv function, which allows you to access single previous known environment variables, and b) using $_ENV, which is only filled when the php.ini-directive 'variables_order' does contain an 'E', which is sometimes not the case (on my machine that is default). What i need is a method to list the environment variables no matter what php.ini directives are set (unless access to environment-variables is not explicit denied). This is needed for things like error-handling for storing the state of the environment to late better understand what is going on or environment-handling in unit-testing. When you cannot list all environment variables, you can never be sure that some variables will hide from you and ruin your testing. Access (even write-access) to data which you cannot get a complete overview from is just a crippled concept to me which needs to be fixed. Implementing such a feature should not be too hard. Test script: --------------- <?php /** * Such a function should exists in PHP. * (The assertion is always true. * It should work regardless of variables_order.) * @return array */ function listenv(){ assert("substr_count(ini_get('variables_order'), 'E')>0;"); return array_keys($_ENV); } foreach(listenv() as $key){ $value = var_export(getenv($key), true); echo "\${$key} = {$value};\n"; } Expected result: ---------------- $SHELL = '/bin/bash'; $TERM = 'xterm'; $USER = 'root'; $SUDO_USER = 'username'; $SUDO_UID = '1000'; $USERNAME = 'root'; $MAIL = '/var/mail/root'; $PATH = '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'; $LC_MESSAGES = 'de_DE.UTF-8'; $LC_COLLATE = 'de_DE.UTF-8'; $PWD = '/home/username'; $LANG = 'de_DE.UTF-8'; $SHLVL = '1'; $SUDO_COMMAND = '/bin/su'; $HOME = '/root'; $LANGUAGE = 'de:en'; $LOGNAME = 'root'; $LC_CTYPE = 'de_DE.UTF-8'; $LESSOPEN = '| /usr/bin/lesspipe %s'; $SUDO_GID = '1003'; $DISPLAY = ':0'; $LESSCLOSE = '/usr/bin/lesspipe %s %s'; $XAUTHORITY = '/home/username/.Xauthority'; $COLORTERM = 'gnome-terminal'; $_ = '/usr/bin/php'; Actual result: -------------- PHP Warning: assert(): Assertion "substr_count(ini_get('variables_order'), 'E')>0;" failed in /home/gerrit/test.php on line 10 PHP Stack trace: PHP 1. {main}() /home/username/test.php:0 PHP 2. listenv() /home/username/test.php:14 PHP 3. assert('substr_count(ini_get(\'variables_order\'), \'E\')>0;') /home/username/test.php:10 root@username:/home/username# php -r "var_dump(ini_get('variables_order'));" string(4) "GPCS" root@username:/home/username# # this is default-configuration. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=63008&edit=1