Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to persist PHP application setings?

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?

like image 624
John B Avatar asked Feb 08 '09 23:02

John B


4 Answers

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
                )

        )

)
like image 144
Paolo Bergantino Avatar answered Nov 11 '22 11:11

Paolo Bergantino


PHP has functions to read .ini files.

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

like image 25
Ólafur Waage Avatar answered Nov 11 '22 11:11

Ólafur Waage


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

like image 39
gahooa Avatar answered Nov 11 '22 13:11

gahooa


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.

like image 27
Michał Tatarynowicz Avatar answered Nov 11 '22 11:11

Michał Tatarynowicz