SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    702
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Parsing Configuration

    Hi guys,

    I have a file, called simply "config.php" which contains various variables and constants for a website. These values can be viewed in the admin control panel, but cannot yet be changed. This I want to change.

    My question is: Can I easily change a value in a file, or do I need to effectively wipe it and build the whole file from scratch in PHP when it is saved?

    Many thanks for all your help

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,077
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    One possible option would be to store the config values in a database (the database connection values can't be but could be pared from a ini file). You would need to make sure that the database is secure.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    702
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I did that before and I moved away from that solution. I'd prefer to not go back to it. Thanks though

  4. #4
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Antnee View Post
    I did that before and I moved away from that solution. I'd prefer to not go back to it. Thanks though
    Why wouldn't you want to store configuration in a database? Personally, I love the thought of storing config data in an SQLite database. But that's just a personal preference. You could use MySQL if you'd like to store configuration parameters and regenerate the config.php file based on the database values.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    702
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well the site is built on MySQL, so we have that database already there, no problems, but I like to keep all the configuration together in one place. Since the database connection information has to be in a file, and not the database, then I keep the rest with it. It also makes it easier for me to test a site. The site in question for example, has a single database, and two sets of files; one on the web server, and one on my PC, which is running WAMP. I can modify the configuration easily on the local copy without messing with the remote one, yet still use the same database. I know there are plenty of ways of doing it using the database still, but they're not as simple.

    Ultimately, I'm not asking for advice on how best to store configuration settings, I'm asking if there's a way for me to update a particular variable/constant in an existing file. Forget that it's a config file if you like, just imagine that it's any old file that needs to be amended. If I have to recreate the file from scratch every time then that's fine, I just wondered if there was a better way.

    Thanks for all the suggestions nevertheless.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I don't know if you are aware of the phpclasses site, but they have a section on config files classes, might give you some ideas.

    ( you have to have a login and suffer some awful ads, and check the licences carefully, but its a good place to get ideas - be aware each class authors' idea of security may not be the same as yours )

    I see you don't want ini config, but I certainly used one once from that list, I can't say whether it was very secure but i was the only one using it, it was a breeze.

    If anyone else wonders about which config system to use, I found this tutorial a few days ago to be quite good.

    Compares, ini, xml, php arrays and using a database.

  7. #7
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    702
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Now that article is really interesting I think, thank you.

    It suggests that if you're only talking about a small number of parameters that a database is pretty slow, compared to an INI file or a PHP file, but when you're using thousands of settings then suddenly the database makes sense. That to me seems obvious, what with indexing etc, but I guess you could argue that the amount of time that the database took in the small-scale example is so negligible that it probably doesn't matter which you pick anyway.

    Very interesting to see that XML was so fast with the large number of parameters however!

    Back on to the subject, what I was thinking was to read the config file in to memory when I want to edit it, find the value that I want, replace it and then write it back to the file. I assume it's possible to write to a .PHP file in the same way as I do a .TXT file?

  8. #8
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I keep 2 separate configs - a file, and a database table.

    The file I use for environment specific information - paths, database connection info, etc. Anything else (eg, that modifies how the site behaves, email addresses to use for certain things etc) go in the database table.

    Keeps the changes needed to the config file to a minimum

    It should be possible to write out a php file exactly the same way as a text file. You just have to construct it and output - a php file is just a text file after all, with a different extension.

  9. #9
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    702
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thought so, thanks. I'm still determined to stick with the config file for this project, but that article has given me some interesting ideas for future work.

    Thanks again

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    In memory will always be alot faster, I don't recall whether he mentioned APC cache, but I used that on one project and that was incredibly quick by comparison.

    I have never used sqlite heap tables option either.

    With the dbase, one thing he doesnt talk about is how you are likely to output absolutely nothing if your dbase is down, so if you do a lot of caching, then using a dbase - as you say - for not too many settings may not be the best bet.

    Ditto the xml findings he made.

    I assume it's possible to write to a .PHP file in the same way as I do a .TXT file?
    Absolutely. Write any kind of file you like, if I had APC I would very likely use PHP arrays which were written to a file, say, config.php ( much like the PhpMyAdmin setup ) and get that into memory.

    Otherwise, as I am working on now, I have a boot up file for each database table ( for those that I want CRUD access to, for form generation etc ) and that I keep as an ini file, dead simple - thought that info about xml has given me a few ideas on how I can associate many more attributes to a field.

    hmmmm...

  11. #11
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    England
    Posts
    702
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    At present I'm using constants for almost everything in a config.php file. The only thing that isn't constant is the template, as that can be different on every page.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •