How to use getenv() in php and SetEnv in a .htaccess with a compiled php-cgi on a shared host

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');


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