|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-09-04 08:17 UTC] 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 30 07:00:01 2025 UTC |
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.