View Full Version : Setting Up A Configuration file

07-12-2007, 05:09 PM
As I venture into building web apps I'm noticing that I can really benefit from some type of a configuration file. I know every project's config will be different but I was wondering what are some of your common variables and/or functions that you place into a config file?

For instance I know one thing that seems to be constant in a config file is the base url. (This example is from Drupal (http://drupal.org/node/6554))
Actually, after testing this on my own system it doesn't work as I expected. Can anyone offer any explanations what this code actually does so I can decide whether or not I should remove this from the post. This post is intended to help and not confuse matters.

$base_url = 'http' . (isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] == 'on' ? 's' : '' : '');
$base_url .= '://' .$_SERVER['HTTP_HOST'];
if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) {
$base_url .= "/$dir";

What is your way of creating this and what other tricks and tips can you share?

07-12-2007, 05:17 PM
I've also noticed that some a lot of config files store there settings in an associative array with the setting as the index.

$config['root_url'] = $_SERVER['DOCUMENT_ROOT'];

07-12-2007, 06:23 PM
Personally, I use the MVC (Model-View-Controller) design pattern when I develop web applications. If you haven't heard of MVC, check the following URL http://en.wikipedia.org/wiki/Model-view-controller

Initially...it was quite difficult for me to develop applications using the MVC design pattern specially in PHP...but once I have got used to it...I found building applications using the MVC model...is an ideal solution...as presentation, business logics, data access etc...are sperated and...reusing designs, classes, functions etc. became straight a forward operation.

One of the things that I find most agitating when trying to customize open source PHP applications...is that I have to go through loads of different files..and the structure is different from one open source application to the next.

Not just that, I also found problems with my own style of programming...specially when I was performing some maintenance work on some old projects. What I found most difficult is all these include() functions on top of every file...where you will have to open one, two, three some times more files just to get an idea of what is going on.

As a result, I would strongly recommend using the MVC design pattern...as your code will slowly become more and more structured. Also, it is much easier to update a website...locate the file where you need to perform the updates...etc.

Ruby on Rails is perhaps the best example of using MVC model.....and most of my PHP projects now utilize a similar architecture.

I think the Zend Framework makes use of the MVC design pattern..and it is a very promising project....as a lot of developers are starting to adopt it...and it could well become the standard for developing open source PHP projects.

07-12-2007, 09:37 PM
Yes I've recently had a photo gallery like that. With a MILLION different includes really frustrating considering I was only using it as a base to extend upon.

I'm familiar with separating the application into different layers as I've had to use Smarty off and on since '03. (I'd rather not use Smarty anymore.)

If I only had PHP 5 I'd give the ZEND framework a shot. I just got a new MoBo so I'll put PHP5 on my new cpu when I finish the build.

I was kinda hoping that this thread would turn into a discussion topic where people could share their tips.

07-13-2007, 03:38 AM
For me... a config file should make the application portable, e.g. I should be able to move the code from my test server to a live server and only have to change that one file.

So that would include mostly paths (library and base), database connections and email addresses.

...but might also include constants which tell other scripts what is available on this server ... e.g DEFINE('IMG_ENGINE',1);//IMAGICK=1 GD=2

I don't put any common includes or functions in the config file... however ...since the configuration file is called on each and every page view I also utilize it to sanitize GET,POST,COOKIE vars etc instead of having to do that wherever I use such data in individual classes and functions.
Again, since it is always called I initialize sessions in my config.

Those 2 routines are common to almost anything I do, so I am happy for them to live in config, anything else gets included elsewhere as appropriate.

07-13-2007, 07:00 PM
I also utilize it to sanitize GET,POST,COOKIE vars
I actually edited the word "function" into that paragraph based on the very same reason.

I have my sanitizing procedure as a function call that also analyzes the GPC vars. I actually got this idea coincidentally enough from you in our discussion about your pagination class :cool: .

In addition, I think I'm going to start adding a boolean variable for maintenance mode. So, whenever I want to put my site under construction I can just change the value and have it redirect or whatever.

I think thats the type of thing I was looking for when you said you add constants such as what type of image engine the server is running.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum