16

Just putting in context to clarify the main question:

On my development machine, PHP5.3.1 is installed on Apache as a module, I use SetEnv APPLICATION_ENVIRONMENT development in my application's root .htaccess file. It is then easily retrievable from any php script with getenv('APPLICATION_ENVIRONMENT').

On the production server, on a sharedhost (dreamhost), I compiled myself php5.3.1 since it was not directly supported. Everything works fine except that getenv('APPLICATION_ENVIRONMENT') returns false.

In the sharedhost root .htaccess for my domain, I use this .htaccess file

Options +ExecCGI
AddHandler php-cgi .php
Action php-cgi /cgi-bin/php.cgi

<FilesMatch "^php5?\.(ini|cgi)$">
    Order Deny,Allow
    Deny from All
    Allow from env=REDIRECT_STATUS
</FilesMatch>

Options -indexes

php5.cgi resides in /cgi-bin and works very well. Of course in my application's root folder I have another .htaccess defining:

SetEnv APPLICATION_ENVIRONMENT production

But when using getenv('APPLICATION_ENVIRONMENT') it returns false, any idea how to resolve this?

1
  • Just guessing: Maybe your host does not allow this directive to be overridden in .htaccess files? Commented Jan 5, 2010 at 18:20

3 Answers 3

15

Ok I finally got it. On dreamhost, it is possible to use fastcgi and therefore declare environment variables with it. It consists of just adding this simple script

#!/bin/sh
export PHP_FCGI_CHILDREN=2
exec /home/USERNAME/YOURDOMAIN/cgi-bin/php.cgi

Which is where my compiled PHP5.3.1 was located. chmod 744 on that file called dispatch.fcgi which will be allowed more memory by dreamhost's watchdog.

After that I added to my domain's .htaccess the following:

Options +ExecCGI
AddHandler fastcgi-script fcg fcgi fpl
AddHandler php5-fastcgi .php
Action php5-fastcgi /dispatch.fcgi

now in the application's root I have another .htaccess with:

SetEnv APPLICATION_ENVIRONMENT staging

In a php script is is retrievable via getenv('REDIRECT_APPLICATION_ENVIRONMENT');

4
  • oh thank you!!! I was losing hair trying to set an environment variable in .htaccess. I was able to finally do it by changing PHP from running as a CGI application to FastCGI. Commented Nov 14, 2013 at 3:48
  • 5
    Why are you using getenv('REDIRECT_APPLICATION_ENVIRONMENT') instead of getenv('APPLICATION_ENVIRONMENT') ?
    – Erin Geyer
    Commented Jun 14, 2014 at 16:20
  • For this to work for me, I had to leave off the prepended "REDIRECT_" part of the environment variable.
    – Erin Geyer
    Commented Jun 14, 2014 at 17:24
  • +1 Had an issue with Magento 1.5 setting environment variables with SetEnvIf Host. The variables MAGE_RUN_CODE and MAGE_RUN_TYPE were being set in the PHP super global $_SERVER as REDIRECT_MAGE_RUN_CODE and REDIRECT_MAGE_RUN_TYPE. This answer got me looking in the right direction.
    – Garry
    Commented Mar 3, 2018 at 17:31
10

For the SetEnv directive to work, your hosting service must have the mod_env module activated...

But, even if it's activated, maybe you don't have to permission to use SetEnv.

Just to be sure the problem is not in your code, you might want to check the ouput of phpinfo() : at the bottom of the page, there should be a section containing environment variables as seen from PHP -- if yours is not in there, it's not a good sign for you...

1
  • Yep you are right, I am not finding anything under PHP variables under the production server and I do on my development machine. I am contacting their support for this and will try to provide a workaround answer. Commented Jan 5, 2010 at 19:50
0

In order for $_ENV to work, I had to reconfigure variables_order = "GPCSE" in php.ini. By default, it did not include E for $_ENV, it was originally variables_order = "GPCS"

This directive determines which super global arrays are registered when PHP starts up. 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 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

Not the answer you're looking for? Browse other questions tagged or ask your own question.