Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts

    Setting Up A Configuration file

    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)
    Edit: 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.

    PHP Code:
    $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?
    Last edited by StupidRalph; 07-12-2007 at 04:21 PM.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #2
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    I've also noticed that some a lot of config files store there settings in an associative array with the setting as the index.
    PHP Code:
    $config['root_url'] = $_SERVER['DOCUMENT_ROOT']; 
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #3
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    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.
    http://framework.zend.com/

  • #4
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    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.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Quote Originally Posted by firepages
    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 .

    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.
    PHP Code:
    $config['maintenance'
    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.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •