SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to store settings?

    Hi,
    What would be the best way to store some general settings like db connection info, site title, max items per page etc. in a quite big php application? So it will be easy to change these settings for the admins who might not understand so much about php?

  2. #2
    SitePoint Guru rageh's Avatar
    Join Date
    Apr 2006
    Location
    London, Formerly Somalia
    Posts
    612
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The admin does not need to change db connections, does he? Altering the db connections such as db_name, db_password and db_name is something the backend programmer has to deal with.

    But for title, max items per page etc can be achieved by scripting. In other words, normal settings.php would be let an admin person change these settings in his browser after login.

    another inflexible way is to use the old good define() function. But editing these values mean that the admin has to deal with the bare code, and that can introduce a lot of errors. So using php coding is your best bet.
    ------------------

  3. #3
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually now I have declared all the settings as constants in one single file which is then included. And in this case the admins have to be able to change all kind of stuff including db connection related because they will be the ones who ever will change those and they are in control of all sql servers etc.

    I have seen many kind of implementations from a .ini file which is then parsed by a script, form based web management and also creating an abstract class which holds the constants for settings.. any ideas about these 3? Pros and cons?

  4. #4
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So nobody has opinions on different styles of storing application settings?

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Here's a rather interesting article TeNDoLLA regarding the storage of configuration settings.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Here's a rather interesting article TeNDoLLA regarding the storage of configuration settings.
    Thanks! This answered perfectly to what I was looking for. Great article.

  7. #7
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are not using a database or PHP file to store your config, be absolutely sure you place the config.ini or config.xml in a place where you can not access it from the web.

    This means either storing it outside the webroot or protecting it with .htaccess.

  8. #8
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by simshaun View Post
    If you are not using a database or PHP file to store your config, be absolutely sure you place the config.ini or config.xml in a place where you can not access it from the web.

    This means either storing it outside the webroot or protecting it with .htaccess.
    Yup, atm. I am storing them in php file and probably will keep it that way.

  9. #9
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I usually use is a table with two columns:

    Code:
    | attribute....... | value........ |
    -----------------------------------
    | home_title...... | 'Home ti....' |
    | max_per_page.... | 10........... |
    | ................ | ............. |
    First thing I do is I fetch the whole settings table and put it in registry so the settings are then available throughout the whole website.

  10. #10
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    If you are anything like me with a fixation on creating new languages out of XML...
    Might try and make a generic configuration file.

    Code XML:
    <?xml version="1.0" encoding="utf-8"?>
    <config xmlns="urn:xmlns.logical:config" xmlns:enc="urn:xmlns.logical:config.encryption">
        <!-- Full Qualified Name: config.<store.name>.(system|user)[.<group.name>[.<option.name>]] -->
        <store name="production">
            <system>
                <group name="location">
                    <option name="domain" value="example.com"/>
                    <option name="root" value="{__BOOTSTRAP_DIR__}../"/>
                </group>
                <group name="database">
                    <option name="dns">
                        <packet type="special">
                            <!-- Would automaticly be decrypted during parsing -->
                            <enc:encrypt algo="aes:256">
                                <enc:key ref="urn:key.store:{config:system.location.domain}.database"/>
                                <enc:data encoding="base64"><![CDATA[...]]></enc:data>
                            </enc:encrypt>
                        </packet>
                    </option>
                </group>
            </system>
            <user>
                <group name="cookies">
                    <option name="domain" value=".{config:system.location.domain}"/>
                    <option name="path" value="/"/>
                    <option name="expire" value="{php:strtotime( +1 month )}"/>
                </group>
                <option name="wddxTest">
                    <packet type="wddx" encoding="none"><![CDATA[<wddxPacket ...]]></packet>
                </option>
            </user>
        </store>
        <store name="development" ref="production">
            <system>
                <group name="location">
                    <option name="domain" value="example.loc"/>
                </group>
                <group name="database">
                    <option name="dns" value="sqlite:{config:system.location.root}/system/datastore/main.sqlite"/>
                </group>
            </system>
            <user/>
        </store>
    </config>

    * user element contains configuration for application data, while system is for...well system wide.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •