Page 1 of 1
Best way to store application settings without MySQL?
Posted: Mon Apr 06, 2009 4:20 pm
by silverspy18
Hi all. I am pretty new to PHP, and I am currently working on an application that requires several settings to be stored, including usernames/passwords, attributes for each user account, interface preferences and other options. What would be an efficient way to easily retrieve and and change these settings without using a MySQL database? Any suggestions would be greatly appreciated. Thanks.
Re: Best way to store application settings without MySQL?
Posted: Mon Apr 06, 2009 4:27 pm
by pickle
A database is perfect for that.
If you don't want to use MySQL because you don't have access to it, try SQLite.
Re: Best way to store application settings without MySQL?
Posted: Mon Apr 06, 2009 6:31 pm
by silverspy18
I didn't want to use MySQL mainly because I didn't want to configure databases for every server I needed to install the application on, but rather just copy over the code and have it up and running. I'll have to take a look at SQLite.
Re: Best way to store application settings without MySQL?
Posted: Mon Apr 06, 2009 6:43 pm
by estamand
You could always use XML. The Filezilla FTP Server uses XML for the config & user accounts. Have a look at it to get an idea of how they set it up.
Re: Best way to store application settings without MySQL?
Posted: Mon Apr 06, 2009 7:01 pm
by Apollo
XML is way too much work for this. Simply create a settings class (or just an array) with all the values and stuff you need. Save that with
Code: Select all
file_put_contents("config.txt",base64_encode(serialize($yourSettings)));
and load with
Code: Select all
$yourSettings = unserialize(base64_decode(file_get_contents("config.txt")));
Can't get much easier than that

Re: Best way to store application settings without MySQL?
Posted: Mon Apr 06, 2009 7:48 pm
by Eran
ini and csv are good formats for configuration data. PHP has native functions to deal with both
http://www.php.net/fgetcsv
http://www.php.net/function.parse-ini-file
Re: Best way to store application settings without MySQL?
Posted: Mon Apr 06, 2009 8:09 pm
by silverspy18
Thanks Apollo, I took your idea and wrote a test script that worked well.
I see that PHP has functions that can easily parse ini and csv formats. How can I write new settings? Does PHP has native functions to do this, or would I have to write my own?
Re: Best way to store application settings without MySQL?
Posted: Mon Apr 06, 2009 8:21 pm
by Eran
For CSV there's fputcsv -
http://www.php.net/fputcsv
For ini you would have to write your own functions, but it's a simpler format overall
Re: Best way to store application settings without MySQL?
Posted: Mon Apr 06, 2009 9:12 pm
by silverspy18
Thanks for the info pytrin, but I think I'll stick with Apollo's idea as it is easier for me to implement.
However, I am not very familiar with classes. Can I create a class that is essentially a single variable that contains several arrays?
Re: Best way to store application settings without MySQL?
Posted: Mon Apr 06, 2009 9:38 pm
by Eran
serializing is a good idea and I use that often, bear in mind however that serialized strings are not very human readable. Consider this if you want those configuration files to be editable with a text editor
Re: Best way to store application settings without MySQL?
Posted: Mon Apr 06, 2009 10:53 pm
by estamand
Human readable is where XML shows it's strenghts
Re: Best way to store application settings without MySQL?
Posted: Mon Apr 06, 2009 11:33 pm
by silverspy18
I don't plan on having to edit the configuration file anyway, I only need the application to understand it. At this point I want simplicity.
Sorry if what I am about to say is confusing, but I'm having a hard time figuring out classes. What I want to do is store several different arrays (one for registered user names, another for their passwords, etc.) inside a single class that can then be passed to serialize() and be stored inside a single configuration file.
I have written this class that reads the configuration file:
Code: Select all
class app_config {
function app_config() {
if (file_exists('config.txt')) {
$this->app_settings = unserialize(base64_decode(file_get_contents('config.txt')));
} else {
echo("The required configuration file does not exist.");
}
}
}
This code is from a test script that I wrote to made the configuration file that is read by the class above:
Code: Select all
class settings {
function settings() {
$this->usernames = array($_REQUEST['username']);
$this->passwords = array($_REQUEST['username']=>$_REQUEST['password']);
}
}
$_settings = new settings();
if (isset($_REQUEST['chgset'])) {
file_put_contents('config.txt', base64_encode(serialize($_settings)));
echo("<b>Changes made</b><br />");
}
What I am having trouble with is knowing how to access the 'usernames' array in the app_config class? This is what I have:
Code: Select all
if (isset($_REQUEST['cklogin'])) {
if (in_array($_REQUEST['user'], "What do I put here to access the 'usernames' array?")) {
echo('User identified');
}
}