1

I am making a custom CMS in PHP and I want to know what the best way would be to create a config file for it. I want it where I can change the variables from within the admin panel I am going to add. I have not messed with the filesystem functions before or any other file functions so I am not sure what would be the best approach. Thanks!

legobear154
  • 431
  • 2
  • 6
  • 16

4 Answers4

2

If you want a human-readable config file format then look into parse_ini_file(). You'll need to be able to write to the file too, see: create ini file, write values in PHP. There's a PEAR Package Config_Lite that seems like it should work too.

If readability doesn't matter then save it to a database.

Community
  • 1
  • 1
nachito
  • 6,975
  • 2
  • 25
  • 44
2

An .ini file can be structured quite well and you'll be able to update just sections of. Compared to a straight PHP configuration it can be edited with an easier syntax.

To parse the .ini file into a PHP array, use the function parse_ini_file()

konsolenfreddy
  • 9,551
  • 1
  • 25
  • 36
  • ini files are more what I am looking for, thank you! I wanted it to be human-readable so I could easily change values in case I can't get into the database for whatever reason. And like you said, I can just update sections. – legobear154 Jan 29 '12 at 20:20
1

If you want the variables to be changeable via an admin web interface, store those variables in the database like any other CRUD data.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • i have always wondered if it was necessary to store/how to store into the database a single line data, like say "footer credits" and "site name". a config file is better in this situations. – Joseph Jan 29 '12 at 20:31
  • @Joseph Why would you need a web interface to change the site name? It's hardly something you'd do on a regular basis. – ceejayoz Jan 29 '12 at 20:34
  • the part of the purpose of a CMS is to make it easy for non-techy people to edit site settings as well as provide access to a lot of off-site configurations from within the admin section of the site. – Joseph Jan 29 '12 at 20:43
1

The best idea I think is to use MySQL to store the data.

However, if you cannot do that for some reasons, then I would suggest to make it an XML file and then you can get variables from SimpleXML, and such, you can view all and put their values to a form. Then, the destination PHP could easily make a string like "<val1>".$_POST["value1"]."</val1><val2>".$_POST["value2"]."</val2>". Finally, it would save the file through simple file system functions, which you can learn with googling "php file write".

Or another idea is the parse_ini_file() which is already mentioned.

If you don't understand something, ask. Or Google!

axiomer
  • 2,088
  • 1
  • 17
  • 26