symbol heatxsink.com blog  ·  archive  ·  about  ·  Feed feed

Apache environment variables in PHP

Monday, April 04, 2011 09:11 AM

<VirtualHost *:80>
    ServerName some-domain.com
    ServerAdmin admin@some-domain.com
    DocumentRoot /var/www/some-domain/htdocs

    SetEnv DEVELOPER_MODE 1

    ErrorLog /var/log/some-domain.com-error_log
    CustomLog /var/log/some-domain.com-access_log common
</VirtualHost>

Apache environment variables could make your code a lot less complex. In the above sample Apache configuration xml you can see that DEVELOPER_MODE is set to 1. This is sweet so you could have different values across different environments (dev/stage/production).

In PHP you can access environment variables like:

$developer_mode = $_SERVER['DEVELOPER_MODE'];

In rainbow-php there's a static function in ApplicationConfig.class.php:

public static function IsDeveloperMode() {
    $developer_mode = (bool) $_SERVER['DEVELOPER_MODE'];
    return $developer_mode;
}