php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63008 Need reliable way of listing environment variables.
Submitted: 2012-09-04 08:17 UTC Modified: 2018-07-07 16:48 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: phpbugs at addiks dot de Assigned: cmb (profile)
Status: Closed Package: *General Issues
PHP Version: 5.4.6 OS: Ubuntu/Debian
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: phpbugs at addiks dot de
New email:
PHP Version: OS:

 

 [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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-04 08:32 UTC] 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 09:04 UTC] rasmus@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 09:42 UTC] phpbugs at addiks dot de
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.
 [2018-07-07 16:48 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2018-07-07 16:48 UTC] cmb@php.net
As of PHP 7.1.0 getenv() can be called without argument to yield
an associative array of all environment variables.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 15:01:34 2025 UTC