What is the best way to save PHP application settings?

I have a small application that I am developing so that I can give / sell to others. I want to save some settings and create an admin interface to change them. What would be the best way to store them? The DB table seems redundant for the 10-20 settings that I have, and I want the search for these parameters to be as fast as possible. Is a flat file another viable option? What are the pitfalls associated with using a flat file? What would be the fastest / easiest way to interact with a flat file storing multiple keys and values?

+6
database php web-applications file-io settings
source share
6 answers

I often use PHP parse_ini_file for this.

So, if you write a .ini file with this:

 ; This is a sample configuration file ; Comments start with ';', as in php.ini [first_section] one = 1 five = 5 animal = BIRD [second_section] path = "/usr/local/bin" URL = "http://www.example.com/~username" [third_section] phpversion[] = "5.0" phpversion[] = "5.1" phpversion[] = "5.2" phpversion[] = "5.3" 

And read it with this PHP code:

 define('BIRD', 'Dodo bird'); $ini_array = parse_ini_file("sample.ini", true); print_r($ini_array); 

You will get this output:

 Array ( [first_section] => Array ( [one] => 1 [five] => 5 [animal] => Dodo bird ) [second_section] => Array ( [path] => /usr/local/bin [URL] => http://www.example.com/~username ) [third_section] => Array ( [phpversion] => Array ( [0] => 5.0 [1] => 5.1 [2] => 5.2 [3] => 5.3 ) ) ) 
+15
source share

PHP has functions for reading .ini files.

http://is2.php.net/manual/en/function.parse-ini-file.php

+2
source share

It really depends on the type of “settings” you want to save. Are they “bootable” settings, such as the database host, port, and login? Or is it application settings specifically for your application?

The problem with providing the administrator interface with writing a file to the file system is the permissions required to write to the file. Each time you open a web server to write files, you increase the likelihood that an error in the code can lead to a serious escalation of privileges.

Databases are designed to be read and written without causing potential security risks to the system.

We use "generated" PHP files to store static configuration data (for example, database access information). It is created by the user script utility on the command line. After that, all non-static information is stored in the database table. The database table, in turn, is easy to update from the administrator area. When updating the application, you easily expand and update.

It is also much easier to centralize the “data” that needs to be backed up in one place.

Is it possible to use memcached or something similar to speed it up?

Just a couple of thoughts ...

+2
source share

I think XML through SimpleXMLElement is very useful for this kind of thing.

Configuration (config.xml):

 <config version="1"> <foo>value</foo> <bar> <baz>Boo</baz> </bar> </config> 

Code to read:

 $config = simplexml_load_file('config.xml'); $version = (int) $config['version']; $foo = (string) $config->foo; $baz = (string) $config->bar->baz; 

Code for writing to file:

 $config = new SimpleXMLElement('<config version="1"/>'); $config->foo = 'value'; $config->bar->baz = 'Boo'; $config->asXML('config.xml'); 

The main disadvantage of using flat files is that it can lead to possible data corruption if the script crashes or is edited at the same time.

+1
source share

Consider an object (or array) of PHP serialized to a file. There is no code for parsing, fast enough, extensible. The only caveat is to think of a strategy to “update” the settings file as the code grows to support further settings.

0
source share

I just use a PHP array for configuration files.

 <?php return array ( 'user' => 'name', 'pass' => 'word', 'one_day' => 60 * 60 * 24 ); ?> 

Some of the advantages are that, as morendil said, no parsing is required, it is as fast as it is, you can store all kinds of complex variables / equations, and you can do the following:

 <?php $config = include 'path/to/file.php'; ?> 
0
source share

All Articles