How to save configuration settings for a web application?

I have some site metadata that I would like to change ... for example, in my application, if the system administrator did not want to use the "Inventory" part on the site, he can disable it and it will disappear from the main site.

So, I was thinking, maybe I could make a table in my database called "meta" and insert the values ​​(or tuples) there! Then, if the module was disabled, the script will update the line and set "module x" to 0, and I would do with it, right?

Also, this seems like an awful lot of overhead (creating an entire table and saving it, etc.) just for a set of values ​​... Basically, my solution sounds like dragging a square anchor into a round slot.

A quick look at the drupal database gave nothing, and I assume that they use the configuration file on the server itself? If so, I don’t know exactly how saved values ​​in a .cfg file (for example) can be read by a web application, and I don’t know how such an application can save information to a file. I would appreciate your understanding if you solved this problem earlier.

I use primarily PHP, by the way.

Thanks in advance!

+3
source share
4 answers

I often saw how this was possible using the config array:

$config["admin_email"] = " admin@mydomain.com "; $config["site_name"] = "Bob Trinket Store"; $config["excluded_modules"] = array("inventory", "user_chat"); 

Then you can check:

 if (!in_array("inventory", $config["excluded_modules"])) { // include inventory logic } 

Of course, this is a little back. In fact, it would be wiser to explicitly declare the included modules, rather than negative ones. You then refer to this config.php in your project for loading and responding to various configurations.

You can also implement this as a database table by creating at least two fields:

  • Option
  • Value

Where option can be "excluded_modules" and its corresponding value will be "inventory, user_cat". However, to be honest, this method is a bit messy and can lead to some disappointments in the future.

+2
source

I know your question is “How do I read / write to a separate file on the server from a web application”, but I decided that I would consider one of the assumptions you made. There is nothing (also) wrong with saving your config in the database.

I saw projects (with lots of traffic and good uptime), as well as tons of IT that store it that way = P), which stores the configuration in a database, more or less, as you described. If this is a separate table and you do not have the whole crazy failure / splitting scheme, then this is not so much overhead.

The database has many functions, in addition to storing data and a large infrastructure around it. If you use the database for your configuration, you can use any mechanism that you have to deploy / backup the database, with little additional cost. You can also use the built-in permissions mechanism and any available revocation features.

Edit: However, if you access this configuration on every page display, however, you can have a bottleneck :) Everything about your design. One solution is that if you have a consistent web service, you can re-check it every X seconds.

+2
source

You have two options basically - either put it in a database table, or in a flat configuration file (maybe PHP, maybe XML). With the latter, to make it editable from the page, you will have to (1) deal with the dirty access problems of the OS file, (1) apply the correct permissions for the files every time you create a site, and (3) parse and generate PHP / XML code. With the database, all you need is a simple query, so I would definitely agree with that.

As for large projects using this approach, I know that phpBB stores most of its configuration in a database (excluding passwords, the last time I checked).

+1
source

I prefer to work with ini files as a configuration that sits in front of the public_html folder. I think this gives me more flexibility and grouping var and, if necessary, creates a separate ini for modules, etc.

+1
source

All Articles