My_config.ini vs my_config.php

When working, we use the .ini file to set the variables before calling the rest of the framework (I think it goes

function getConfigVars(){ //read my_config.ini file .... //call framework } 

and I always wondered if there was such an opportunity for this.

It seems to me that you need to write access rules so that people do not look at it from the Internet, and php should parse it and understand it.

So why use my_config.ini and not my_config.php? This is not like someone should touch it after it is configured, and it seems more convenient to just call the variables and be able to automate your IDE file, wherever you use ini variables / analyze it for errors.

+14
php configuration-files ini
May 05 '09 at 4:31
source share
4 answers

Zend Framework contains configuration analyzes that analyze files written in ini format ( Zend_Config_Ini ), it looks like this is what you are using.

The configuration file should not be at the root of your document, and if it is not at the root of your document, then rewriting rules are not required, since no one can access it in any case.

The INI format is specialized to provide both the possibility of a hierarchy of configuration data keys and inheritance between sections of configuration data. Hierarchies of configuration data are supported by dividing keys into a period or period character (.). A section can be distributed or inherited from another section, following the section name with a colon (:) and the name of the section from which data should be inherited.

From the Zend_Config_Ini page.

The Zend Framework uses it to allow you to have several configuration options: one for the stage, one for development and one for production. It also makes it easy to tune the database parameters for production as well as for development and have two very different settings. The various paths set in the ini file, where they are located, are located. This greatly facilitates the transition of code from development to production, knowing that immediately everything that is development will be disabled.

Of course, this would be possible with a PHP script, but this would require more analysis of various configuration variables, as well as performing if / then checks, while using parse_ini_file () does all this automatically for you.

Other answers have also indicated that non-programmers may need to change the variables and / or something on the website that is set as the configuration variable (for example, the name of the site that is used in the site layout). INI files are easy to understand and read even for those who have never programmed before.

Example from the website I'm working on now:

 [production] phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 includePaths.library = APPLICATION_PATH "/../library" bootstrap.path = APPLICATION_PATH "/Bootstrap.php" bootstrap.class = "Bootstrap" resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers" resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts" resources.db.adapter = "PDO_SQLITE" resources.db.params.dbname = APPLICATION_PATH "/../data/db/users.db" resources.view[] = [staging : production] [testing : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 resources.db.params.dbname = APPLICATION_PATH "/../data/db/users-testing.db" [development : production] phpSettings.display_startup_errors = 1 phpSettings.display_errors = 1 resources.db.params.dbname = APPLICATION_PATH "/../data/db/users-dev.db 

This simplifies the collection of multiple data sets for various environments in which code can be executed.

+9
May 05 '09 at 4:40
source share

For those who come to this question because they want to know if there are any performance differences between using an INI file that needs to be parsed and a PHP file that is just included (and can be cached by PHP): Yes, there are differences, but they are so small that it does not really matter.

My test case is a config.ini file with 20 key / value pairs and a config.php with the same 20 key / value pairs written as it defines. The PHP version is 5.4.9 on Ubuntu Linux 13.04.

 key1 = value1 ... key20 = value20 

against.

 <?php define("key1", "value1"); ... define("key2", "value20"); 

Two test cases, including configs:

 <?php $CONF = parse_ini_file("config.ini"); 

against.

 <?php require_once "config.php"; 

I tested performance with ab -c 25 -n 10000 .

Result without PHP cache:

 ini: Requests per second: 2660.89 [#/sec] (mean) php: Requests per second: 2642.28 [#/sec] (mean) 

Result with PHP APC cache:

 ini: Requests per second: 3294.47 [#/sec] (mean) php: Requests per second: 3307.89 [#/sec] (mean) 

I ran the tests several times, of course, the numbers will change every time, but the consensus is this: config.ini little faster when the PHP cache is not used, config.php little faster when the PHP cache is used. But the difference is so small that the solution should not be based on performance.

+12
May 17 '13 at 7:11
source share

Of course, your question raises a fair point.

A few points in favor of .ini files:

  • Use a file with a different language . If you ever wanted to have Perl, Python, Ruby, etc. The script to do something that is especially simple with this language and you need to access the project settings, you are out of luck if you saved your settings in a PHP file.

  • Human data editing . Although you rejected it in your question, intentions or not, it is very likely that someone may be there, and this may not be a technical person. The INI format is much less scary than the PHP code, even if it's just a bunch of variable declarations

  • Update settings . I think it is much easier to create a new INI file rather than writing a PHP file. However, this is quite subjective, but worth mentioning.

  • The relationship between variable settings . It is quite simple / intuitive to give your settings a hierarchy with an INI file. Although this is also possible with PHP, it is not so neat and can become unsightly if you are trying to make deeply nested associative arrays for storing information.

In addition to this, the INI's β€œneed to protect against network access” act is not relevant to most scenarios, since most PHP projects (at least the ones I am part of) contain enough code from the root folder, and settings are usually go there.

+10
May 05 '09 at 4:35
source share

Well, maybe it’s easier so that not the programmer can change the configuration variables ... If necessary at your workplace.

I found that careful placement of <?php and ?> May interfere with its display, while parse_ini_file () will still get the corresponding data from the file.

The best way to protect it is to place it above docroot and deny access to * .ini in your server settings.

+1
May 05 '09 at 4:34 a.m.
source share



All Articles