Basics of the Configuration File
Structure of Configuration File
Here is a quick way to create a configuration file from the web, used to initially setup an application or that can be expanded to allow users to modify the configuration later.
We will take advantage of the PHP's ability to include code into an existing script. Instead of creating an initialization file that must be read and parsed, we let PHP do the parsing. The configuration file consists of a series of variable definitions. Our example script could be used to initially setup a database driven web application, such as a message board, etc. It may also be extended to allow updating of application settings at any time.
Our example will contain settings frequently used in PHP applications: values used for connecting with and querying a database and a filesystem path. Here is what the resulting configuration file might look like.
<?php
// Do not edit this file. Generated by admin script.
// Database settings
$db_user = 'me';
$db_pass = 'noneofyourbizness';
$db_host = 'localhost';
$db_name = 'mydb';
$db_table = 'mystuff';
// Paths
$base_path = 'home/users/me/mystuff';
?>
Our goal is to accept values from a web form and write changes to a configuration file.
For example, if you wanted to create a setup script for a database driven web application, users would go to a form page with the following inputs:
db_user Database user name.
db_pass Database password.
db_host Database host name.
db_name Database name.
db_table Name of table application uses.
base_path Path to folder containing files
of interest to the application.
Pages: 1 2 3 4 | Next: Writing the Configuration File » |