Edit report at https://bugs.php.net/bug.php?id=63008&edit=1
ID: 63008
Comment 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:
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?
Previous Comments:
------------------------------------------------------------------------
[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