A Custom Configuration File For PHP Programs
|In a previous tutorial, you learned how to connect to the MySQL database server. Here is the code we discussed and as you will remember, the three important pieces of this code are the host, username and password which are hard-coded into the connection code. |
OK so far, but one of the goals of writing efficient PHP program code is for the program to be as universal as possible. That is, you want your program to be usable and customizable for anyone. Therefore you need an easy way for any end user of your program to use his or her own host, username and password. So hard-coding them into the connection code is not very user friendly or efficient. The end user will need to go through the entire program code and recode his host, username and password. This can be very time consuming.
The answer to this problem is to turn these into variables and place the $variable within the connection code instead of the values themselves. Let's call the three variables $host, $username and $password. Now we can replace these variables into the original code.
Every time this connection code is used in the program, it will use the same three variables. But we need a convenient way for the user to set the values for these variables. That's easy, we will just use these three lines of code.
Now the only thing left is to decide where to place this code. Of course, you could place it within the program itself just above the connection code but that would not get us the results that we want. A better idea is to place the code in a special file called a configuration file which you can "call in to" any program using the include_once() or require_once() statements. By using this special file, the value of the variables can be changed at any time at this one location and the change will be reflected through the entire program. We will name this file config.php3 and place it in the same folder as the other program files for the calendar program.
But let's make our life even easier by adding a fourth variable to the config.php3 file. We will be using a database for many of our tutorials and it would be nice to set a variable called $name_of_database to the name of the database we wish to use. Let's say we want to use a database which is named "calendar". We will add this line of code to the config.php3 file.
$name_of_database = "calendar";
Here is a sample config.php3 file.
Note for Personal Activities Calendar Program--Because the calendar program is for personal use and security is not an issue considered in the program, it is all right to place the config.php3 file in the same folder as the program files. However, it you were writing a program that did have security issues it would be better to place the config.php3 file in a folder not accessible by anyone without administrative privileges.
This site needs an editor - click to learn more!
Editor's Picks Articles
Top Ten Articles
Content copyright © 2019 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.