Home  >  Article  >  Backend Development  >  How to store configuration information in ini file?

How to store configuration information in ini file?

黄舟
黄舟Original
2017-05-20 17:04:011756browse

How to store configuration information in ini file?

Certain information that is reused across sites (such as passwords, paths, and variables) is best stored in a file so that if the code needs to be moved to another site, You only need to modify the settings once, instead of hundreds or thousands of modified codes. So how do you store these configuration information in the specified file? How do you read this configuration information?

The simplest way to store configuration information is to create variables in an .ini file, and then use the parse_ini_file function to include this file in the code. The function can also parse files in the same format as php.ini, including , create a config.ini file below, its content is as follows:

;This is a mysql configuration file 
;Comments start with ";",as in php.ini
host = localhost
daname = db_database
username = root    
password = root
charset  = usf8

Apply the parse_ini_file function to read the data in the configuration file config.ini, the code is as follows:

<?php
header("Content-Type:text/html; charset=utf-8");
$ini_array = parse_ini_file("config.ini");
print_r($ini_array);
$ini_array = parse_ini_file("config.ini",true);
print_r($ini_array);
?>

The output result is as shown in the figure below Display:

How to store configuration information in ini file?

When storing the configuration file, it is best not to store the configuration file in the Web root directory, because the configuration file usually contains user name and password information. If you configure If the file is stored in the root directory of the Web, it will be easy for "uninvited guests" to access and steal important information. Therefore, it should be stored outside the server, so that it will be difficult for "uninvited guests" to access the file.

If the configuration file must be stored in the Web root directory, be sure to include a file directive in the .htaccess file to restrict users who can access this type of file!

This is the end of the introduction on how to store configuration information in files. I believe that everyone can master this knowledge point. You can contact your local area to consolidate the knowledge you have mastered. !

The above is the detailed content of How to store configuration information in ini file?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn