Multipurpose PHP Config Files Revisted

Published 25 November 04 by Justin French, 7 comments

In the original article, I used a quick regular expression on the server name to decide if we were in development (like on our desktop computer), or on the “live” production server. It works fine, but for TextThing, I was looking for something a little more generic.

Rails tests for a server IP of 127.0.0.1 (which works for most people), but doesn’t work for me and my set-up.

So what I decided is that if the server IP address ($_SERVER['SERVER_ADDR']) matches the client IP address ($_SERVER['REMOTE_ADDR']), this indicates that you’re browsing to a website hosted on your desktop machine (like Apache + PHP running on OS X).

<? 
$live = ($_SERVER['REMOTE_ADDR']==$_SERVER['SERVER_ADDR']) ? 0 : 1;
?>

It’s a little bit faster than the old regular expression, and I think a little more generic than checking for a specific host name or IP address. In short, it should work for more people out-of-the-box.

But wait, there’s more!

In the previous version, I set a variable $live to either true or false. In practice, it felt more intuitive to test for $dev in my code, so here’s the revision (note the values 1 & 0 are swapped around):

<? 
$dev = ($_SERVER['REMOTE_ADDR']==$_SERVER['SERVER_ADDR']) ? 1 : 0;
?>

I also got sick of making the $dev variable global within each function that needed to test it. So why don’t we define a constant instead? They’re available in the global name space by default, and they can’t redefined, so a constant makes much more sense.

<? 
define('DEV', ($_SERVER['REMOTE_ADDR']==$_SERVER['SERVER_ADDR']));
?>

Or, if that confuses you a little, here’s a more obvious version:

<? 
if($_SERVER['REMOTE_ADDR']==$_SERVER['SERVER_ADDR'])
  {
  define('DEV',TRUE);
  }
else
  {
  define('DEV',FALSE);
  }
?>

Revisiting the original example config file, it now looks like this (the values being reversed, and $live replaced with DEV).

<?
// are we in DEV?
define('DEV', ($_SERVER['REMOTE_ADDR']==$_SERVER['SERVER_ADDR']));
// some config directives
$dbhost = 'localhost';
$dbname = (DEV) ? 'dog' : 'cat';
$dbuser = (DEV) ? 'root' : 'justin';
$dbpass = (DEV) ? 'secret' : 'squirrel';
$debug = (DEV) ? 1 : 0;
$docroot = (DEV) ? '/path/to/docroot' : '/path/to/docroot';
// what about some PHP directives?
$errors = (DEV) ? E_ALL : E_NONE;
error_reporting($errors);
?>

So there we go – a slightly slicker, more generic test for the development server, and we can delete a few global statements from our functions, using the constant DEV isntead.

Less code and less configuration is a beautiful thing.

Update: As pointed out by Hadley Wickham in the first comment, my define() statement was a little but long winded, and has since been revised. Thanks Hadley!

Options

What is this?

portrait of Justin

This is the online home of Justin French, a designer & web application developer located in Melbourne, Australia. I like finding ways to make things work better. I like clarifying and simplifying. I like to understand how you understand things.

» read more

Subscribe to my feed

Follow me on Twitter

@justinfrench

More Notebook Articles

Show more notebook articles

Search