Blog

Detecting your environment using server variables

Posted by on Feb 15, 2010 in Tutorials | 0 comments

This article explains how you can use server variables to run websites on multiple servers without having to manually reconfigure key settings.

Like many web developers I use a single configuration file to define key website constants (file paths, database login etc.) and that file is included at the top of every page. Many of the constants can be sensitive to the working environment through so I may need one configuration for local development work and another when the website is uploaded to the live server.

If you’ve only ever done very basic server-side scripting you may be wondering why some settings need to vary. To show you the kind of things I’m taking about here’s an example of the first few lines of a typical configuration file in one of my websites.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$environment = 'live';
if ($environment == 'live') {
  define('HTTPPATH', '/');
  define('ROOTPATH', '/home/sitedir/html/');
  define('DB_NAME', 'live_dbname');
  define('DB_USER', 'live_dbuser');
  define('DB_PASS', 'live_dbpass');
} else {
  define('HTTPPATH', '/sitedir/');
  define('ROOTPATH', '/home/ed/public_html/sitedir/');
  define('DB_NAME', 'dev_dbname');
  define('DB_USER', 'dev_dbuser');
  define('DB_PASS', 'dev_dbpass');
}

So that snippet of code shows an if-statement which loads the relevant website settings depending on the value of $environment. There are a few ways to switch between different configurations.

Method 1: Manually set the correct configuration values

The most obvious way of switching configurations this is to manually change $environment whenever you upload the configuration file to the server. This is just something else to remember while editing your site though and is easily overlooked when you do a bulk upload to the server. It would be far better to automate the procedure.

Method 2: Rudimentary environment detection

Why bother manually updating the $environment when PHP can do it automatically? To do that we need a method of detecting where the site is running and that’s where the predefined $_SERVER array comes in.

You’re probably already familiar with $_SERVER but if not, all you need to know is that it’s an associative array containing information passed to PHP from the server itself. In the context of this article it contains useful information about the server address and script paths, for example here’s a few elements from a typical $_SERVER array.

KeyValue
HTTP_HOSTlocalhost
SERVER_NAMElocalhost
SERVER_ADDR::1
SERVER_PORT80
REMOTE_ADDR::1
DOCUMENT_ROOT/Users/ed/Sites
SCRIPT_FILENAME/Users/ed/Sites/index.php

We can compare one or more of those array elements to a predictable value in order to deduce the website’s current location. For example if I know the development version of my website is always run from the HTTP location of ‘localhost’ I could define $environment as follows:

1
2
3
4
5
if ($_SERVER['SERVER_NAME'] == 'localhost') {
  $environment = 'dev';
} else {
  $environment = 'live';
}

Now it doesn’t matter whether this file is running on my local development server or on the live server, it will automatically define the correct constants without me having to manually alter it every time it’s moved from one location to another.

Method 3: Environment detection using server variables

Using the default data provided in $_SERVER isn’t always ideal though because if the environment you’re testing against ever changes the website may fail. Also it can become a bit of pain writing unique new conditions for every website you develop – wouldn’t it be good if there were consistent values within $_SERVER that we could rely for a given server regardless of the website that’s being loaded?

As a real world example I am currently developing a single piece of code that will drive multiple websites, each with their own domain and living within their own directory on the server. It’s further complicated because the code must run on a live server, my office PC and my laptop (the latter two being development environments).

The tidiest way I’ve found to solve this problem is to set my own server variables, which is actually really simple.

When I configure a server now, I put the following code into the main Apache configuration but it could also be added to a specific site’s vhost file, or even a .htaccess file (assuming that it’s not synchronised with the development version).

SetEnv ENVIRONMENT devbox

Now in my configuration file I can check this value just as if it was any other server variable.

1
2
3
4
5
6
7
8
// select active environment
if ($_SERVER['ENVIRONMENT'] == 'devbox') {
  $environment = 'devbox';
} else if ($_SERVER['ENVIRONMENT'] = 'laptop';) {
  $environment = 'laptop';
} else {
  $environment = 'live';
}

To make life a little easier you may find that you don’t even need to add this server variable to your live server. In most cases just having it defined on the development server(s) will be sufficient.

You may be able to think of other uses for these custom server variables but I think these is amongst the most useful of them.