views:

386

answers:

6

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?

+2  A: 

PHP has functions to read .ini files.

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

Ólafur Waage
oo, beat by 10 seconds. :)
Paolo Bergantino
With copy paste from the php docs :P cheap
Ólafur Waage
I went the extra mile, kind sir! ;)
Paolo Bergantino
mile? More like extra feet. :D
Ólafur Waage
+14  A: 

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
                )

        )

)
Paolo Bergantino
+1 for all the sample code
cbrulak
Awesome response! Speedy and complete! Thanks!
SkippyFire
Wow, I did not know this! Many thanks!
strager
I use a singleton to handle configs, but this is very useful information.
Syntax
This wouldn't allow you to modify the settings from the code though, right?
SkippyFire
A: 

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.

Morendil
+1  A: 

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.

Pies
+1  A: 

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...

gahooa
A: 

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';
?>
Mario
Actually parsing *is* required, and one might argue that parsing and executing PHP code is more complicated task than extracting strings for .ini file. Still, I agree that using PHP is the easiest way.
che
I mean no manual parsing, plus the array can be cached in APC ;)
Mario
Instead of an array, wouldn't it be better/faster to use a class?
SkippyFire
Can you give me an example?
Mario
In that config file, setup a singleton class, and return an instance of it? Would that work?
SkippyFire
Not sure where you're going with the singleton idea. What I was trying to simply say is, store a returning array in the config file instead of plain text like an ini. No variable declaration...
Mario