DEV Community

Supun Kavinda
Supun Kavinda

Posted on

Production-friendly Configuration Files in PHP

When developing a PHP apps, we often need to keep some configurations saved in constants. In that way, we can access it anywhere in our application.

But, the problem is that we need to have different configurations for development and production (+ may be testing). Here's a simple trick that you can use to do it effectively.

You will need 3 files (Normally I save include files in the /inc directory).

  • inc/config.php - This is the file you are going to include
  • inc/config.server.php - Production configurations
  • inc/config.dev.php - Development configurations
  • inc/config.default.php (Optional) - Default configurations (Common for both production and development)

In config.php, first we include the correct config file according to the server name. Make sure you replace the domains with your production and development domains in the following code. Then, we can include the default configurations file.

config.php

<?php
if ($_SERVER['SERVER_NAME'] === 'production.com') {
    include_once 'config.server.php';
} else if ($_SERVER['SERVER_NAME'] === 'localdomain.com') {
    include_once 'config.dev.php';
}

include_once 'config.default.php';
Enter fullscreen mode Exit fullscreen mode

Each config file can contain code as following.

// database config
define('DB_HOST', 'localhost');
define('DB_USERNAME', 'root'); 
// ...

// domain config
define('PROTOCOL', 'https://');
// ...

// and any thing
Enter fullscreen mode Exit fullscreen mode

The default config file can contain configurations which are common to both production and development.

define('MAX_USERNAME_LENGTH', 15);

// etc.
Enter fullscreen mode Exit fullscreen mode

Now, in any other PHP file where you need configurations to be included, you can just include the config.php file.

<?php
include_once '/path/to/config.php';
Enter fullscreen mode Exit fullscreen mode

You are done!

You may already have used this technique previously. But, if you haven't I'm happy you learned something new. I think this is the best way to save configs as there's no need to rename files in production server each time you upload, which makes no sense. (I have seen some people do that)

:)

Top comments (3)

Collapse
 
antoniocs profile image
AntonioCS

Please stop doing this.
Use a modern php framework (Symfony, Laravel, etc) and follow their convention on how to store config data.

Using things like include or require should not happen in modern PHP.
Stop doing this!

Collapse
 
jbdano profile image
jbdano

I like this implementation.

@antoniocs Can you elaborate? I don't see why an entire framework is needed for such a seemingly minor objective.

Collapse
 
antoniocs profile image
AntonioCS

You talk about applications, in my mind an application, even if small can benefit from a framework like symfony (especially 4 which is quite lean and small).
Even if you don't want a framework I am sure you can find a package that will help with this.
Trying to do this on your own is fine, but create a post telling others to do this and that this is good is wrong.