Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my $_ENV empty?

I'm running Apache/2.2.11 (Win32) PHP/5.3.0 and I did the following in my .htaccess file:

SetEnv FOO bar 

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

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

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

It appears I can get it using getenv('FOO'), so maybe I should just use that instead. But I am still a bit curious to what causes this. Is this a Windows issue? Or what is going on?

like image 576
Svish Avatar asked Sep 23 '10 17:09

Svish


People also ask

What does $_ env mean in PHP?

Introduction. $_ENV is another superglobal associative array in PHP. It stores environment variables available to current script. $HTTP_ENV_VARS also contains the same information, but is not a superglobal, and now been deprecated. Environment variables are imported into global namespace.

What should .env files contain?

env file should be particular to the environment and not checked into version control. This. env. example file documents the application's necessary variables and can be committed to version control.


2 Answers

Turns out there was two issues here:

1. $_ENV is only populated if php.ini allows it, which it doesn't seem to do by default, at least not in the default WAMP server installation.

; 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 set the variables_order back to EGPCS, $_ENV is no longer empty.

2. When you use SetEnv in your .htaccess, it ends up in $_SERVER, not in $_ENV, which I gotta say is a tad confusing when it's named 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 is not affected by the PHP setting for $_ENV Additionally it seems to do so insensitive to case, which might be useful.

var_dump(getenv('os'), getenv('env'));  // string 'Windows_NT' (length=10) // string 'dev' (length=3) 
like image 68
Svish Avatar answered Oct 08 '22 01:10

Svish


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

IIRC in a standard Apache+mod_php install on Windows, the only way to change variables in $_ENV is to change Windows' environment variables (see this). This can be significant when dealing with PHP extensions on Windows, because some of them (eg: php_ldap) are only configurable through environment vars on $_ENV.

like image 31
NullUserException Avatar answered Oct 08 '22 02:10

NullUserException