How do I store settings for a site?


I want to develop a site that has some configuration options, for example:

  • Keep the default or custom logo
  • Allow new user registration
  • Authorize anonymous comments on posts

How should these settings be maintained? Remember that there is a control panel where the user can change them.

asked by anonymous 01.12.2015 / 15:09

2 answers


Depends on the way you need it.

The simplest way is to put this information in global variables or encapsulate it in some way (an array , a class, etc.). This usually works well if it can not be changed by users. You can even let the user change, but it's not worth the hassle to do it right.

If the user (other than the programmer or the application installer) can configure this, as indicated in the question, then it would be better to store this somewhere else. It may be a simple file, or better yet would be put into a database. Especially if the application already uses one.

What I would do

In general, a single table with one column for each possible configuration is usually sufficient. Most columns will be boolian, but nothing prevents you from having any configurable information.

If you want to generalize more, you can create a table that allows the user to define their own settings. Of course this helps little if the code does not know what to do with them.

A simple table suffices for general configuration. If you have more specialized settings per user, for example, you have to have a column indicating who the specific value belongs to, and the application needs to know how to read it according to the user.

This is the important part to store. Then you have to think about whether it's worth having some optimizations to avoid database access every time you need one of these settings. You have to think about whether you should have a way to abstract the access and allow the storage method to be changed in the future.

At last, there is enough that needs to be thought out. But the question has no details, I can only respond in general terms.

01.12.2015 / 15:21

I usually create a class app_config , and its instance immediately in the index of the application with the current directory and I indicate a configuration file config.ini.php with the settings, in its content I put:

;die("Se quiser, coloque uma mensagem de erro aqui caso o usuário tente acessar esse arquivo");


PS: I used the example database that is as generic as possible, there are cases that you will need to use another type of authentication in your bank and will not store passwords so, it all depends on your project , if it is something simple, there will be no problem. I already explain why .ini.php

Creating the .ini.php file and placing these comments above is useful when you or your client uses a shared hosting and you can not place outside the public_html of the project, there is no risk of the user downloading .ini.php , even not containing any rule in htaccess .

And here is the class app_config to read the file.

class app_config
    private $ini;
    private $projeto_dir;

    /** método construtor, recebe o caminho completo do sistema para o 
        arquivo ini, e então verifica sua existência **/
    public function __construct($arquivo, $projeto_dir)
            $this->ini = parse_ini_file($arquivo,true);
        catch (Exception $e) 
            die('Não foi possível encontrar o arquivo de configuração solicitado, a execução não poderá continuar');
        $this->projeto_dir = $projeto_dir;

      * Retorna o valor da sessão e parâmetro solicitado
      * Requer um arquivo de configuração válido 
      * @name get
      * @access public
      * @return string 
    private function get($sessao,$parametro)
        return $this->ini[$sessao][$parametro];

      * Retorna o usuário do banco de dados
      * @return string
    public function get_db_usuario() 
        return $this->get('db','usuario'); 

      * Retorna a senha do banco de dados
      * @return string
    public function get_db_senha() 
        return $this->get('db','senha'); 

So when you need it, you can inject your instance into some class that uses its methods.

// A instância da index pega o diretório do projeto com dirname(__FILE__)
    $config = new app_config('local/para/sua/config/config.ini.php', dirname(__FILE__));
    $projeto = new projeto_exemplo($config);

Within your class you intend to inject these settings, have an attribute of type app_config to receive the object we are going to pass by parameter.

This technique of injecting what you need into a class is known as dependency injection .

Within the example, we can then call the methods of app_config .

$db = $this->config->get_db_usuario();

The class I passed is just a sketch, mine is huge and very specific to my project, but that's the logic I use.


  • Do not create global objects.
  • Does not use constants that may be visible to the entire project.
  • It is completely object-oriented.
  • Use the encapsulation.
  • Can read .ini files outside and within public_html
  • Depending on your design, it can be reused.


  • It is more laborious than a simple global constant, but hard work in this case is welcome.

You can also define set / get methods to get information from the database, just implement methods that do this directly in that class.

02.12.2015 / 12:10