Build a configurable PHP application in the correct way

Source: Internet
Author: User
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... this article illustrates several ways 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 ensure that the user can set the database logon and password in a safe way, so that the materials will not be made 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.

Myapptempdir = foo

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:

% php ini1.php foo %

Great. But why cannot I use the standard INI function to obtain the value of the myapptempdir configuration item? I have studied and found that in most cases, custom configuration items cannot be obtained using these methods. However, the get_cfg_var function is 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

 

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, though this is easier to read INI files, it is almost impossible to write INI files securely. 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 reset the include 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, so I can add the require PHP file from this directory without adding the path to the require statement.

Configuration in PHP

An alternative to storing configuration entries in the INI file is to use a simple PHP script to keep data. The following is an example.

Listing 4. config. php

 

The code for using this constant is shown above.



Article URL:

Reprint ^ at will, but please attach the tutorial address.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.