PHP application configuration mode

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, 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.

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.phpfoo%

Great. But why not use the standard INI function?myapptempdirWhat 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_varFunctions 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 resetincludePath to Add configuration items, as shown below.


Listing 3. ini3.php

     

In this example, I add my local mylib directory to the include path.requirePHP file without adding the pathrequireStatement.

 

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.