Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store a PHP App's Settings?

Tags:

php

mysql

What is the best approach to storing a group of global settings for a custom PHP application? I am working on a personal project (first major one really), and need a method of storing key-value pairs for recording overall settings for the application.

Things to store as...

  • Website's Global Name.
  • Theme (just a variable, or path to theme)
  • etc

Should I just keep them in one table? If so what is the best way to query them from a boostrap? Besides doing a single query for each desired setting.


UPDATE: Yes a .ini or parsing an include file would be nice, and I know how to do it that way. But I wanted to know what would be the best approach to storing them in MySQL with everything else.


UPDATE2: The reason I ask this also is I plan for a lot of these settings to be changeable through the Administrator interface. So if you were to change the Title of the site, it would be updated right away, which I figured would be best to do through SQL, thus needing setting inside the DB.

like image 325
Urda Avatar asked Mar 03 '10 02:03

Urda


3 Answers

For a single, small, simple site, I'd just put config in a PHP file. Keep it simple. PHP probably doesn't parse anything faster than it parses PHP. If you use APC, the compiled bytecode is even cached -- although the bytecode is then re-executed for every request. For a small config file, this bytecode execution should take very little time; for a very large file, it might take a bit longer.

For high-traffic sites with large configs, caching your config data in APC (e.g. as a single array) is a good idea -- at the very least, you save the overhead of actually executing the statements in your config.php file. Notably, facebook does this. When you're serving many requests per second, hitting the disk to read a config file (using parse_ini_file, an XML parser, etc.) on every request is out of the question.

For my current project, we host many sites, each with their own config. Each site had both a database and a config file; however, making sure you're always using the right config file with the right database can become a headache. Additionally, changes would require changing things in two places -- the db and the config. Forgetting one or the other always caused problems, and it happened far too frequently.

We moved the config into the database, so that you can't possibly separate a db from it's correct config, and any code changes only require updating the database. The data from the config table is also aggressively cached in APC, so we query it rarely.

So, to recap:

  1. Small site: just use a config.php file
  2. Very large site: cache in APC
  3. Multiple sites: store config in database to reduce administration overhead; cache in APC to reduce database hits
like image 62
Frank Farmer Avatar answered Oct 18 '22 15:10

Frank Farmer


Have you thought about putting them in a .php file and including it on the pages you need to use them? Give the variables a unique name so avoid naming conflicts.

Since you'll be using them repeatedly in your PHP application, this would be most ideal. This also avoids the need to make database calls if you were to store them in a database.

AppSettings.php

<?php
$_SITENAME_ = 'MyWebsite';
$_THEME_ = 'Theme/Path';
?>

UPDATE:

I assume you want these settings to be editable via a web page and don't want multiple DB Queries since these settings will change, but not too often?

One approach I personally took was to serialize the AppSettings table and store it in a XML file. Of course, every time the table is updated, the table would be reserialized and stored in the XML file. Then I created a separate class that parses the XML file and returns the specific values I needed.

like image 7
Omar Avatar answered Oct 18 '22 17:10

Omar


We just use

$siteConfig['db_name'] = 'database1';
$siteConfig['site_name'] = 'Stackoverflow';

In a included php file. Putting the values in a array helps with name conflicts.

like image 2
Jase Whatson Avatar answered Oct 18 '22 16:10

Jase Whatson