What is the best way to persist PHP application setings? What is the best way to persist PHP application setings? database database

What is the best way to persist PHP application setings?


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 = 1five = 5animal = 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                )        ))


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