Site Configuration: Config File
Apr-11, 2005 06:43pm
The first step in building a PHP coded, dynamic content based Website, typically known as a Content Management System (CMS), is to create a configuration process, whereby you, the Administrator, can change how the site works.
This first step is, in it's most simpilist form, a primitive, text based configuration file, also known as an iniitialization file, or INI file.
There is the PHP function:
array parse_ini_file ( string $filename [, bool $process_sections ] )
which is adequate for most purposes. It has some shortcomings though, two of which I found unacceptable for my usage. These are:
Here is my version of this function:
array configfile ( mixed $file [, array $thisconfig = NULL, bool $translate = TRUE ] )
The basic differences are:
The main advantage of a INI file for site configuration as opposed to using a table in MySQL or something is that this way can be coded in only a few minutes. Here is an example. Obviously, this is a "do nothing" example. But, let me try to elaborate later.
This first step is, in it's most simpilist form, a primitive, text based configuration file, also known as an iniitialization file, or INI file.
There is the PHP function:
array parse_ini_file ( string $filename [, bool $process_sections ] )
which is adequate for most purposes. It has some shortcomings though, two of which I found unacceptable for my usage. These are:
- Reserved words must not be used as keys (eg. null, yes, no, true, and false).
- Non-alphanumeric characters in values need to be enclosed in double-quotes (").
Here is my version of this function:
array configfile ( mixed $file [, array $thisconfig = NULL, bool $translate = TRUE ] )
The basic differences are:
- Sections are always processed.
- An array to append to can be passed ($thisconfig).
- Values can contain any characters including equals (=).
- $file can be an array of "key = value" entries to process.
- Constants are not parsed in values.
- Superglobals are parsed in values (eg. key = {$GLOBALS['value']}).
- Keys can be translated to lowercase.
The main advantage of a INI file for site configuration as opposed to using a table in MySQL or something is that this way can be coded in only a few minutes. Here is an example. Obviously, this is a "do nothing" example. But, let me try to elaborate later.
; file.ini
sitename = Basic Concept This Way
admin = administrator@bigwigs.com
sitename = Basic Concept This Way
admin = administrator@bigwigs.com
<?php
$config = configfile("file.ini");
if ($config['option'] == 1)
...
?>