Reading Time: 1 minute, 12 secs

Managing Application Settings in PHP

There are multiple ways to save application settings/configurations in PHP. You can save them in INI, XML or PHP files as well as a database table. I prefer a combination of the latter two; saving the database connection details in a PHP file and the rest in a database table.

The advantage of using this approach over the others will be apparent when developing downloadable scripts, as updates will not need to modify a configuration file of an already setup script.

To start create a table containing 3 fields: auto increment ID, setting name and setting value:

The following function updates the value of a setting by supplying it with the setting name and the new value.

The GetSetting function returns 1 or more values depending on the parameter passed. You can pass a setting name in a string or multiple settings in an array of strings. The latter will save you multiple trips to the database to perform a task that requires more than 1 setting like sending an email which needs the email server, protocol and credentials.

Here is an example of using the GetSetting function to initialize an email object.

NOTE: This code does not filter the values sent to SaveSetting(). To prevent SQL injection and XSS attacks please make sure you check the values before saving them and also after reading them using GetSetting().

0 Likes

Leave a Comment.