I have a small application that I'm developing, that I may want to give/sell to others. I want to persist some settings, and create an admin interface to modify them. What would be the best way to store them away? A DB table seems like overkill for the 10-20 settings I'll have, and I want the retrieval of these settings 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 interface with a flat file storing multiple keys and values?
I often use PHP's parse_ini_file
for this.
So if you write an .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 in 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
)
)
)
Consider a PHP object (or array) serialized to a file. No parsing code to write, fast enough, extensible. The one caveat is to think of a strategy to "upgrade" the settings file as your code grows to support further settings.
I think XML via SimpleXMLElement is very useful for that kind of thing.
The configuration (config.xml):
<config version="1">
<foo>value</foo>
<bar>
<baz>Boo</baz>
</bar>
</config>
The code to read it:
$config = simplexml_load_file('config.xml');
$version = (int) $config['version'];
$foo = (string) $config->foo;
$baz = (string) $config->bar->baz;
Code to write it to a file:
$config = new SimpleXMLElement('<config version="1"/>');
$config->foo = 'value';
$config->bar->baz = 'Boo';
$config->asXML('config.xml');
The main pitfall of using flat files is that it could lead to possible data corruption on script crash or concurrent editing.
It really depends on the type of "settings" you want to store. Are they "bootstrap" settings like DB host, port, and login? Or are they application settings specifically for your application?
The problem with letting an admin interface write a file on the file system is the permissions needed in order to write to the file. Anytime you open up the web server to write files, you increase the possibility that an error in your code could allow severe privilege escalation.
Databases are designed to allow reads and writes without introducing the potential system security risks.
We use "generated" PHP files to store static configuration data in (like database access info). It is generated by a utility script by a user on the command line. After that, all non-static information is stored in a database table. The database table, in turn, is easy to update from an Admin area. It is easy to extend and upgrade as you upgrade your applicecation.
It's also much easier to centralize the "data" that needs backed up in one place.
May I suggest using memcached or something similar to speed it up?
Just a couple thoughts...
I simply use a PHP array for config files.
<?php
return array
(
'user' => 'name',
'pass' => 'word',
'one_day' => 60 * 60 * 24
);
?>
Some of the advantages are that, like morendil mentioned, no parsing required, its as fast as it gets, you can store all kinds of complex variables / equations and you can do the following:
<?php
$config = include 'path/to/file.php';
?>