1

Possible Duplicate:
Reading and Writing Configuration Files

Okay, I have a config file, which contains some information that I would like the user to be able to change. I've already thought about holding the information in a MySQL table but this isn't a good idea as I've got my Database class included AFTER my configuration variables and this would mess up my code structure.
Any idea how to read/write variables/specific lines of a file? Here's my global config file.>

error_reporting(E_ALL);

/*
    The parameters used site-wide
    First set are Datbase parameters
    And second set are the salts used to generate strings
*/
$params['db']['host'] = 'localhost';
$params['db']['username'] = 'root';
$params['db']['password'] = 'waffliner';
$params['db']['db_name'] = 'habcms';

$params ['core']['salt1'] = "184962574320355793361124913390";
$params['core']['salt2'] = "13952134426315134482857516025";

$params['core']['timeout'] = "60 minutes";


try {
    include_once("core.inc.php");
    include_once("db.inc.php");
    include_once("user.inc.php");       
} catch(exception $e) {
    echo $e->getMessage();
}

?>

Community
  • 1
  • 1
Joshwaa
  • 840
  • 1
  • 11
  • 22

2 Answers2

3

JSON♥ is your friend.

{
    "db": {
        "host" : "localhost",
        "username" : "root"
        ...
    },
    ...
}
Zirak
  • 38,920
  • 13
  • 81
  • 92
  • 3
    May I ask why the down vote? json is a possibility; clean looking and easy to parse. – Zirak Apr 21 '11 at 16:19
  • 1
    Beware that json files can be downloaded by default --> if it is stored within documentroot, anyone guessing the url of your config file can get the DB login details! – cytofu Jun 06 '15 at 08:18
1

If you want to do so, you need to store you configuration in a non-PHP file.

You can, for example, store your config in an .ini file and read it with parse_ini_file().

Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • Applications like wordpress and vbulletin seem to programatically write configuration to PHP files, so it is possible. This is probably desirable for high traffic applications (>100 requests per second) -- with a cache like APC, you avoid reading and parsing the config file for every request. Still, for low traffic applications, keeping it simple with `parse_ini_file()` is probably preferable. – Frank Farmer Apr 20 '11 at 21:47
  • This is not true. There is no reason that the OP cannot use a plain old PHP array for that purpose. – Gordon Apr 21 '11 at 07:13
  • @Gordon, @Frank Farmer : Of course, but regarding to the request : "Any idea how to read/write variables/specific lines of a file?", .ini file is the easiest way. If you want to propose something plain PHP for this, go ahead. – Matthieu Napoli Apr 21 '11 at 15:36
  • @Matthieu there is a solution using arrays in the linked duplicate – Gordon Apr 21 '11 at 15:40
  • @Gordon : Yes but I wouldn't like to be the one modifying the PHP configuration file generated by var_export or serialize. No comments possible, no formatting... This isn't like modifying a line in the configuration you wrote. It's modifying a part of a raw generated PHP file. – Matthieu Napoli Apr 21 '11 at 15:58
  • @Matthieu but it is formatted. And you can very much add comments to the raw source file either as long as you are not saving the config again. I'd argue if you are saving it again, you are not maintaining it in the source file anyways. Besides, you cannot add comments to an ini file either from PHP, can you? – Gordon Apr 21 '11 at 16:04
  • @Gordon I have ini config files and I do add comments in them (and line breaks too). The question said "Any idea how to read/write variables/specific lines of a file?" so I guess the config file will be overwritten, I'm pretty sure (but not certain though) that you can write a .ini variable only, without modifying all the content of the file. – Matthieu Napoli Apr 21 '11 at 16:08
  • @Matthieu afaik, PHP has not function to write ini files at all. And Zend_Config replaces the entire thing IIRC. And while I am sure you can come up with a tool that only updates the changed lines somehow, I dont see any point in doing so. Either you maintain the config file manually, then you only read it from PHP and then you put comments in the file directly or you dont maintain it manually and then you dont need comments in the config file nor formatting at all. So in any case, you can very much use a full php solution. – Gordon Apr 21 '11 at 16:21