Why is my $ _ENV empty?

I am running Apache/2.2.11 (Win32) PHP/5.3.0 and I have done the following in my .htaccess file:

 SetEnv FOO bar 

If I print the variable $_ENV in a PHP file, I get an empty array. Why doesn't my environment variable appear? Why is it empty first?

I found my variable, but it appears in the $_SERVER . And for some reason, he appears twice, sort of. Why is this?

 [REDIRECT_FOO] => bar [FOO] => bar 

It seems I can get it with getenv('FOO') , so maybe I should use this instead. But I'm still a little curious about what this leads to. Is this a windows problem? Or what is happening?

+72
php environment-variables apache
Sep 23 '10 at 17:15
source share
3 answers

It turns out there were two problems:

1. $_ENV populated only if php.ini allows it , which apparently does not run by default, at least when installing the default WAMP server .

 ; This directive determines which super global arrays are registered when PHP ; starts up. If the register_globals directive is enabled, it also determines ; what order variables are populated into the global space. G,P,C,E & S are ; abbreviations for the following respective super globals: GET, POST, COOKIE, ; ENV and SERVER. There is a performance penalty paid for the registration of ; these arrays and because ENV is not as commonly used as the others, ENV is ; is not recommended on productions servers. You can still get access to ; the environment variables through getenv() should you need to. ; Default Value: "EGPCS" ; Development Value: "GPCS" ; Production Value: "GPCS"; ; http://php.net/variables-order variables_order = "GPCS" 

When I returned variables_order back to EGPCS , $_ENV will no longer be empty.

2. When you use SetEnv in your .htaccess , it ends in $_SERVER , not $_ENV , which, as I have to say, is a little confusing when it is called SetEnv ...

 # .htaccess SetEnv ENV dev SetEnv BASE /ssl/ # php var_dump($_SERVER['ENV'], $_SERVER['BASE']); // string 'dev' (length=3) // string '/ssl/' (length=5) 

3. The getenv function will always work and will not depend on the PHP settings for $ _ENV . In addition, it does not seem to be case sensitive, which may be useful.

 var_dump(getenv('os'), getenv('env')); // string 'Windows_NT' (length=10) // string 'dev' (length=3) 
+95
Nov 22 '14 at 12:36
source share

$_ENV variables are imported from the environment in which PHP runs, and depending on your installation (OS, your server, regardless of whether PHP runs as an Apache module or under FastCGI, etc.), this can vary greatly.

IIRC in the standard Apache + mod_php installation for Windows, the only way to change the variables in $_ENV is to change the Windows environment variables (see this ). This can be important when working with PHP extensions on Windows, because some of them (for example: php_ldap ) are configured only through the vars environment on $_ENV .

+12
Sep 23 '10 at 17:21
source share

REDIRECT_* variables appear if you use RewriteRules. On my server, they also appear that way. This may have something to do with working in FastCGI. And if combined with suexec, this will most likely clear the complete var pool environment. Additional configuration might be required to get them back, PassEnv . As to why getenv () works for you, I have no idea. But all phenomena are specific to your server and php configuration. Ask on serverfault, they need to know.

+1
Sep 23 '10 at 17:24
source share



All Articles