0

I have two web sites using codeigniter that uses the same database configurations(hostname,username,password), just want to ask if the database configuration can be called from a external file? The thing is if i want to change my database root password i have to change the two sites database.php manually, now lets says i have 10 websites this will be a difficult task correct? i just want to change a single file so all sites can use that configuration.

I am using mysql via PHPmyadmin.

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'root',
    'database' => 'MyBlog',
    'dbdriver' => 'mysqli',
KaoriYui
  • 912
  • 1
  • 13
  • 43
  • you can do it this way. create one file anywhere, should be common for all. and call that file using curl from CI hooks, and in the response you should get database credentials. Now thing is how to pass those dynamic credential in databse.php? google for this. This might be the one way, I'm not sure about it. – Rahul Jun 15 '17 at 07:26

1 Answers1

1

If you have need to use one configuration for all sites, you can make the file (i.e. db_config.php) and set ti somewhere to be available to all CI applications.

db_config.php

<?php

return [
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => '',
    'password' => '',
    'database' => '',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
];

Then in your APPPATH.'config/db.php' of every application you should have:

$db['default'] = require('/path/to/db_config.php');
Tpojka
  • 6,996
  • 2
  • 29
  • 39
  • I am trying to include using a url path, but i encountered a problem like this question https://stackoverflow.com/questions/9498885/cant-include-file-on-remote-server – KaoriYui Jun 15 '17 at 09:16
  • PLease tell every important detail. This solution is meant to be used as regular file require. For that you need public place for that file and then it is not safe since it can be read by anyone. Applications are not on same server? – Tpojka Jun 15 '17 at 09:19
  • Sorry about that i tried to use the base_url(), its in the same server, thanks! – KaoriYui Jun 15 '17 at 10:06
  • Don't forget to exclude that file from publicly accessible location. In no case you should access it through web browser. – Tpojka Jun 15 '17 at 10:59