开发者

PHP: Is there a correct method for saving configuration data?

I have a config.inc file i开发者_JS百科n a web application that I am building. It contains an array with configuration values for things like the MySQL database, etc. I would like these to be entered by using a simple form, that asks for the server, login/password for the database, etc, then these get written to the configuration file.

Is there a preferred method of doing this? I am not sure how to write to a file, and update an array.


You just want writing, correct? Is it a serialized array or is it parsed?

One way to read a config file is parse_ini_file(). I wouldn't necessarily call it preferred, but it's a method. You'd still need to write the file.

Another way would to write a "config.inc.php" and just include it in, to write it you'd just output actual PHP code (e.g. $var = "myval";).

This is a way you could write a simple "output" function that took an array of configuration values and output them as name=value, assuming $config was an associative array.

foreach ($config as $name => $value) {
   $output .= $name . '=' . $value . "\n";
}

if (!file_put_contents($filename, $output)) {
    die("Error writing config file.");
}

There's a lot of decent ways to do it. It's really based on your requirements. Does it need to be in a specific format or do you have leeway?


It is not recommended to modify PHP configuration files via your application, you should use CSV files or a database table. In case you want to save it in a CSV file then I suggest you keep a CSV file for each configuration type (e.g CSV file for database configurations) and always overwrite the previous one using file_put_contents

Save data example:

$csvStructure = array("dbUser","dbPassword","dbHostname","dbPort"); // array used for both loading data and saving it
$csvData = array();

foreach ($csvStructure as $field) {
   $csvData[] = $_POST[$field]; // so it'd get $_POST["dbUser"],$_POST["dbPasword"], etc..
}
file_put_contents("filename",implode("\t",$csvData));

Load data example:

$csvStructure = array("dbUser","dbPassword","dbHostname","dbPort"); // array used for both loading data and saving it
$dbConfig = array();
$csvData = explode("\t",file_get_contents("filename"));    
foreach ($csvStructure as $key => $field) { // $key would have the location of the requested field in our CSV data (0,1,2, etc..).
   $dbConfig[$field] = $csvData[$key]; // populate $dbConfig["dbUser"],$dbConfig["dbPasword"], etc..
}


I believe using an ini file is a wise option, because user, password, schema, paths, etc. are things that usually will be modified by hand, so using var_export isn't because modifying it by hand it's not so clean and may crash your application if you make a mistake in the PHP syntax.

But parsing big ini files can be expensive, so it would be OK to cache the ini with var_export() or serlialize(). It's a better choice, I think, and read the ini only when the cache file doesn't exists.


PHP has a dedicated function for this, its called var_export();

Just do:

file_put_contents("config.php",var_export($config,true));


Well, to write a file, fwrite() php function does exactly what you want. From its PHP.NET documentation page (see example below).

Now, on the question as to what to output to that file - I'm assuming that file will have to be included as a configuration .php file into the rest of the project. I'm imagining you'll do something like this - where you're creating strings with PHP code on the fly, based on the submitted form:

$strDatabaseConfig = "\$databaseConfig = array('" . $_POST['login'] . "," . $_POST['password'] . "');";

And here's the snippet for fwrite:

$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    // In our example we're opening $filename in append mode.
    // The file pointer is at the bottom of the file hence
    // that's where $somecontent will go when we fwrite() it.
    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $somecontent to our opened file.
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";
}


Here's one way: wp-admin/setup-config.php from WordPress.


I prefer to have a file with a bunch of define statements.

These are constants globally available (and of course immutable) which is what you need for configuration settings.

Constants offer better memory management and efficiency in reading as they don't need the extra memory required by a variable so that it can be changed.


Let's say your config.inc file looks like this:

$config = array(
    'blah'  => 'mmm',
    'blah2' => 'www',
    //...
);

You want to update it, so you create a simple form, fill text fields with current values. PHP script that overwrites current configuration could looks like this:

$newConfig = ...; // data from form - of course validate it first
$config = ...;    // data from config.inc

$config = array_merge($config, $newConfig);
file_put_contents('config.inc', '<?php $config = ' . var_export($config, true));

And you're done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜