PHP program configuration file (best ?) Practice

Source: Internet
Author: User
PHP program configuration file (best ?) The most primitive approach to practice

When writing a php program, the connection information of the database and cache will be directly put in the config. php file. This method has two drawbacks: 1. during development and debugging, you must first change the connection information to a local one, and then change the connection information back to a remote one when submitting code, which is troublesome and easy to omit. 2. developers can directly view the connection address, account, and password of the online database, which is not secure.

Http://leo108.com/pid-2184.asp

What should I do?

An Http server, such as apache and nginx, is often required to run PHP programs online. Take nginx as an example. in the configuration file, you can use the fastcgi_param command to pass the leo108's blog variable to PHP.

fastcgi_param  DB_HOST  "192.168.1.1";

In this way, the corresponding value can be obtained through $ _ SERVER ['DB _ host'] in the PHP code. PHP

Therefore, you only need to configure nginx in the development environment and online environment to implement the code execution program without modifying the code.

Configuration

Furthermore, you can pass a variable that identifies the current environment, for example

PHP

fastcgi_param  CODE_ENV  "production";

Then, you can execute some different logic in the program based on $ _ SERVER ['code _ env'] (for example, the development environment will print all the error messages, but the online environment will not display them)

Configuration

Is this perfect?

No. two problems are found in actual application: 1. if this PHP program not only provides web services, but also provides cli tools (such as database upgrade scripts), the variables passed by nginx will not be available at this time. 2. it seems that array variables cannot be passed. Http://leo108.com/pid-2184.asp

Solution

To support both web and cli, it is no longer feasible to pass variables through nginx configuration. Can I use php's own configuration?

Leo108's blog

Add the following configuration at the end of php. ini:

Http://leo108.com/pid-2184.asp

[userconf]userconf.db_host=127.0.0.1userconf.db_name=test

After restarting php-fpm, it is found that through ini_get ('userconf. db_host ') the obtained data is empty. Therefore, I checked the php document in detail and found that the custom configuration items should be obtained through the get_cfg_var () function. After testing, the correct value can be obtained through get_cmd_var ('userconf. db_host ') in web and cli modes.

Configuration

Optimization

However, this solution has drawbacks: 1. to modify the configuration, restart php-fpm. 2. Although php. ini supports array data, it is not flexible enough. it is best to directly use php for configuration.

Configuration

So we can. in ini, only one configuration directory path is configured. the configuration files of each program are placed under this directory. The php program starts from php. ini obtains the path of this directory, and then reads the configuration file in php format from this directory. Sample Code: Configuration

$path = get_cfg_var('userconf.dir');$conf = include($path . '/test.php');

In this way, the configuration file is read again every time you access it. you do not need to restart php-fpm when you change the configuration file name. In addition, different sites can coexist on one server by selecting different configuration file names.

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.