Best way to store application settings without MySQL?
Moderator: General Moderators
-
silverspy18
- Forum Newbie
- Posts: 23
- Joined: Sun Apr 05, 2009 5:55 pm
Best way to store application settings without MySQL?
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?
A database is perfect for that.
If you don't want to use MySQL because you don't have access to it, try SQLite.
If you don't want to use MySQL because you don't have access to it, try SQLite.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
silverspy18
- Forum Newbie
- Posts: 23
- Joined: Sun Apr 05, 2009 5:55 pm
Re: Best way to store application settings without MySQL?
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?
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?
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 withand load with
Can't get much easier than that 
Code: Select all
file_put_contents("config.txt",base64_encode(serialize($yourSettings)));Code: Select all
$yourSettings = unserialize(base64_decode(file_get_contents("config.txt")));Re: Best way to store application settings without MySQL?
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
http://www.php.net/fgetcsv
http://www.php.net/function.parse-ini-file
-
silverspy18
- Forum Newbie
- Posts: 23
- Joined: Sun Apr 05, 2009 5:55 pm
Re: Best way to store application settings without MySQL?
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?
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?
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
For ini you would have to write your own functions, but it's a simpler format overall
-
silverspy18
- Forum Newbie
- Posts: 23
- Joined: Sun Apr 05, 2009 5:55 pm
Re: Best way to store application settings without MySQL?
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?
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?
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?
Human readable is where XML shows it's strenghts
-
silverspy18
- Forum Newbie
- Posts: 23
- Joined: Sun Apr 05, 2009 5:55 pm
Re: Best way to store application settings without MySQL?
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:
This code is from a test script that I wrote to made the configuration file that is read by the class above:
What I am having trouble with is knowing how to access the 'usernames' array in the app_config class? This is what I have:
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.");
}
}
}
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 />");
}
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');
}
}