Brian
Brian

Reputation: 2221

Storing, Updating, Retrieving settings for a PHP Application without a Database

I need to be able to store data for a php application in a file. I need to be able to do this without any sort of external dependencies other than PHP itself.

Here are the requirements I have:

I would prefer to not have settings stored in arrays like I mentioned above. Instead I would prefer some access methods: getConfig("Database","Accesssettings","Username") would return 'myDBUsername'. The reason for this is I want to limit the variables I am storing in the global scope.

What would the best way of getting/retrieving these be?

Do the the hierarchy I was thinking possibly an xml file, but I wasn't sure what PHP was like for accessing xml files (particularly the fact that I need to be able to add, edit, and remove). If it should be XML what sort of xml access should I look into.

If it is another format, I would like some pointers to the right direction for what to look into for how to use that format.

Upvotes: 2

Views: 1637

Answers (4)

hobodave
hobodave

Reputation: 29303

Brian, parse_ini_file is what you need.

Ah, I missed the requirement that you'd be editing this via PHP.

In that case there is nothing native to PHP that you can use for this purpose. You'd have to roll your own.

You could save yourself a ton of time by simply using Zend_Config_Ini though. I know you state that you don't want to use anything else, but Zend Framework is structured to allow you to use whatever pieces of it you need. Zend_Config can be used on it's own. You can certainly just add these few classes to your project and let them handle your INI file parsing.

Here is an example using your samples above:

[config]
Database.AccessSettings.Username = myDBUsername
Database.AccessSettings.Password = myDBPassword

You would load and access this as simply as:

$config = new Zend_Config_Ini('/path/to/ini', 'config');
echo $config->Datbase->AccessSettings->Username; // prints "myDBUsername"
echo $config->Datbase->AccessSettings->Password; // prints "myDBPassword"

To edit and save your config you would use the following:

$config->Database->AccessSettings->Password = "foobar";
$writer = new Zend_Config_Writer_Ini(array('config'   => $config,
                                           'filename' => 'config.ini'));
$writer->write();

Edit

Not really sure why people are voting this down based on vog's misguided comments. It is very simple to make this writable by multiple persons by using an exclusive lock. Zend_Config_Writer uses file_put_contents to do it's writing, which has always supported the the LOCK_EX flag, which exclusively locks a file for writing. When using this flag, you cannot have multiple writers attempting to update the file at the same time.

To use this flag with Zend_Config_Writer it's as simple as follows:

$writer = new Zend_Config_Writer_Ini(array('config'   => $config,
                                           'filename' => 'config.ini'));
$writer->setExclusiveLock(true);
$writer->write();

An alternate syntax:

$writer = new Zend_Config_Writer_Ini();
$writer->write('config.ini', $config, true); // 3rd parameter is $exclusiveLock

Upvotes: 3

Rafe
Rafe

Reputation: 3066

DBM files may not be a bad idea. PHP has built in support for the various DBM-file varieties:

http://www.php.net/manual/en/book.dba.php

Upvotes: 0

Nathan
Nathan

Reputation: 215

If you are willing to drop in a library, you could use Spyc and have your configuration in a YAML file.

You could also use PHP's support for SQLite which would mean your database is just a file so no need for a DB server.

Upvotes: 0

Gavin M. Roy
Gavin M. Roy

Reputation: 4671

If you're not hand editing, use serialized data.

Writing config using serialize:

file_put_contents('myConfig.txt', serialize($Settings));

Reading config using unserialize:

$Settings = unserialize(file_get_contents('myConfig.txt'));

You could write a class for modifying and getting values for this data using PHP magic functions __get() and __set().

Upvotes: 0

Related Questions