How to store configurations for php application - xml or ini or db

I have an application written in PHP and there are a lot of custom variables. We implement a function in which the user can create sets of configurations and easily switch between them. How to save my configs? how is xml? in the .ini file? in multiple .ini files? in db?

What will provide maximum flexibility if we add fields in the future? Convenience of coding?

If I use db, I will have to use a separate file from the main application for reasons that should not be included, which made me avoid this. (In addition, we use .mdb files for our db.)

I followed the xml route, but I have problems adding and editing configurations using SimpleXML. In addition, the application must be compatible with php 5.1.6, and I'm a little nervous about some functions.

Never engaged in creating custom ini files ....

A little explanation: users will not touch files - they are non-technical. Therefore, the configuration of the source files will be done by us, but then we write a tool that will record their configuration in any format that we choose.

+10
design php configuration
Apr 28 '09 at 15:55
source share
7 answers

Saving the settings for one database to another database does not make much sense, IMO.

I would choose either an ini file, an xml file, or a php file, as suggested by jmucchiello . Each option has pros and cons. As for xml or ini, xml offers more flexibility, but it is harder to maintain (less readable). If your needs are covered ini, stick with a simpler solution. Use xml only if you need more than what ini files can offer.

As for the php solution, I would rate readability somewhere between ini and xml, but it would be harder to keep the current settings. So, as far as I like this approach, I would not recommend it for your current scenario.

+4
Apr 28 '09 at 16:37
source share

I would save the configuration items in a PHP file. This is the most flexible method I've used.

$config['website_url'] = 'google.com'; 

Then I have the following file structure

 - config/development - config/production 

I have a constant defined early in the software called IN_PRODUCTION when this TRUE value loads production configuration items and vice versa.

It is simple and easy to repair.

-Mathew

+5
Apr 28 '09 at 16:07
source share

In fact, I prefer the PHP executable for configuration. You can do many things in an executable file that you cannot easily accomplish with other solutions. The only serious flaw for the executable is that the administrator must be experienced with PHP in order to safely edit the file. PHP can also describe complex data types in your configuration, because it is code.

DB is a problem because you still need an external configuration file to search and connect to db.

XML overflows configuration.

INI files work, and you can use the PHP ini libraries to read them. See Parse_ini_file (). ini files do not deal with complex data types, but very well.

EDIT in response to Itay Moav (comments will not format this right):

 <?php /* my config file */ $_CFG = Array(); /* problem solved */ $_CFG['key1'] = 'value1'; ?> 
+2
Apr 28 '09 at 16:05
source share

I looked at a post with the same question just a minute ago, here is a good solution in the links. It is good practice to collect useful code. I suppose.:)

Click iniHandler class to get the INI handler class!

+2
Dec 12 '11 at 14:25
source share

I think the answer here will really depend on your individual situation. There are times when each of them will be the best option. For your specific case, xml is probably the best. But where does the xml really shine over the ini file if you have several related options. For example, if you need to hold connection information for a database that has nodes for different parts of a connection under a node, it might be easier to read and maintain for that particular connection. It really depends on the data.

I would also say that it would be easier to work with the xml file if you just pulled out one value and did not load the entire file into memory at the beginning of the application.

+1
Apr 28 '09 at 16:01
source share

I would go to ini files, they are human readable, they are parsed very quickly (and if you cache ...).
Of course, not XML. Less believable, analyzed not so fast.
I would not do DB because of speed.
I would not use PHP vars because of this, so you are using GLOBAL scope ...

0
Apr 28 '09 at 16:12
source share

Write your own configuration class that loads configuration files. A good example is the way CodeIgniter is managed.

http://codeigniter.com/user_guide/libraries/config.html

It loads some default configuration files, or you can load a specific one using the Config class.

You can download the source code and see how they do it for educational purposes.

Another option is to create a php file with a config array from the database. Thus, you can administer all parameters and save the files of the generated files.

0
Apr 28 '09 at 16:41
source share



All Articles