Best way to store application settings without MySQL?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
silverspy18
Forum Newbie
Posts: 23
Joined: Sun Apr 05, 2009 5:55 pm

Best way to store application settings without MySQL?

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Best way to store application settings without MySQL?

Post 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.
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?

Post 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.
estamand
Forum Newbie
Posts: 7
Joined: Sat Mar 21, 2009 1:28 pm

Re: Best way to store application settings without MySQL?

Post 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.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Best way to store application settings without MySQL?

Post 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 :)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Best way to store application settings without MySQL?

Post 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
silverspy18
Forum Newbie
Posts: 23
Joined: Sun Apr 05, 2009 5:55 pm

Re: Best way to store application settings without MySQL?

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Best way to store application settings without MySQL?

Post 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
silverspy18
Forum Newbie
Posts: 23
Joined: Sun Apr 05, 2009 5:55 pm

Re: Best way to store application settings without MySQL?

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Best way to store application settings without MySQL?

Post 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
estamand
Forum Newbie
Posts: 7
Joined: Sat Mar 21, 2009 1:28 pm

Re: Best way to store application settings without MySQL?

Post by estamand »

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?

Post 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');    
    }
}
 
Post Reply