Sergey Goryachev2016-05-23 21:55:23
Sergey Goryachev, 2016-05-23 21:55:23

Another stupid question. How to make your own config file?

Good afternoon.
I say this all the time, and I'll say it again - I'm not a php specialist at all, but I need to do it)
In the index.php file, simple variables are displayed, such as Company name $company_name, Company phone number &company_phone, and so on, there are many variables.
Some of them are clogged through the admin panel, and some I need to hide for personal use.
So that later it would be convenient to change just in one file.
What file to create (what format)? And how to refer to a specific line of a file?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
Vadim, 2016-05-23

create a config.php file (the name is not important)
in index.php, where you have html c header footer content body head
insert the following code at the very beginning of the file:

include 'config.php';

The php config file itself has the following content:
$my_config = array(
  'company' => 'Hello world',
  'author' => 'Ivan Ivanov'

Further in the file where config.php was connected via include.
In the place where you need to display the value from the config:
<?=$my_config['author']; ?>

entermix, 2016-05-23

Tools for working with .ini files of web applications

#algooptimize #bottize, 2016-05-23

include php or json.txt or db

index0h, 2016-05-23

Under configs, there is one very convenient approach: configs in class constants.
The meaning is as follows, create a basic config, for example, DefaultConfig.php, set the settings for the development environment

namespace MyVendor\MyProject\Config;

class DefaultConfig
    const MYSQL_DSN = 'mysql:host=localhost;dbname=dbName';
    const MYSQL_USER = 'some_user';
    const MYSQL_PASS = 'some_password';

You also create a config that will be connected in your project Config.php (git/svn/or whatever you have there is ignored)
namespace MyVendor\MyProject\Config;

class Config extends DefaultConfig

The point is, you can access any settings directly using autoload, while redefining them for production, for example, is very simple - you redefine a constant in Config.php.
Due to this, your connection to the database may look like this:
$pdo = new PDO(

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question