This document illustrates how to create a configurable PHP application. This article also discusses the ideal configuration points in applications, and seeks a balance between over-configurable applications and over-closed applications. If you plan to allow others or companies to use your PHP application, make sure that
This document illustrates how to create a configurable PHP application. This article also discusses the ideal configuration points in applications, and seeks a balance between over-configurable applications and over-closed applications.
If you plan to allow others or companies to use your PHP application, make sure it is configurable. At least, you must allow the user to set the database logon and password in a safe way, so that the materials are not disclosed to the public.
This article describes several technologies used to store configuration settings and edit these settings. In addition, this article provides guidance on which elements need to be configured and how to avoid excessive configuration or insufficient configuration.
Use the INI file for configuration
PHP has built-in support for configuration files. This is implemented through the initialization file (ini) mechanism like the php. INI file, which defines constants such as database connection timeout or session storage. If you want to, you can customize the configuration for the application in this php. ini file. For instructions, I add the following lines of code to the php. ini file.
Then, I wrote a small PHP script to read this configuration item, as shown in listing 1.
Listing 1. ini1.php
When you run this code in the command line, the following results are obtained:
Great. But why not use the standard INI function?myapptempdir
What is the configuration item value? I have studied and found that in most cases, custom configuration items cannot be obtained using these methods. Howeverget_cfg_var
Functions are accessible.
To make this method simpler, the access to the variable is encapsulated in the second function. the function uses the configuration key name and a default value as the parameter, as shown below.
Listing 2. ini2.php
function get_ini_value( $n, $dv ){ $c = get_cfg_var( $n ); return ( $c == null ) ? $dv : $c;}function get_template_directory(){ return get_ini_value( "myapptempdir", "tempdir" );} |
This is a good generalization of how to access the INI file. Therefore, if you want to use a different mechanism or store the INI file to another location, you don't have to worry about changing a large number of functions.
I do not recommend using the INI file for application configuration. There are two reasons. First, although it is easier to read INI files, it is almost impossible to write INI files safely. Therefore, this method is only applicable to read-only configuration items. Second, the php. ini file is shared among all applications on the server. Therefore, I don't think application-specific configuration items should be written in this file.
What should I know about the INI file? The most important thing is how to resetinclude
Path to Add configuration items, as shown below.
Listing 3. ini3.php
In this example, I add my local mylib directory to the include path.require
PHP file without adding the pathrequire
Statement.