'Writing to a config file dynamically using PHP

Implementing a SAAS (multi-tenant) app, I have a situation where an app would need to connect to different databases based on the user who wants to login. The databases are for separate institutions. Let's say MANAGER-A for institution-A, MANAGER-B for institution-B, want to login into their various institutions.

The process I'm implementing is this: There are 3 databases involved: DEFAULT-DB, INSTITUTION-A-DB, INSTITUTION-B-DB. The DEFAULT-DB contains all the login and database credentials of each user. This means that before MANAGER-A can login to his app, what happens is that, first, he will be logged in into the DEFAULT-DB, if successful, his details would be fetched and logged and set as the parameter of the config.php file. This means that connection will be dynamic based on the params fetched and passed by the DEFAULT-DB. My questions are these:

How do I write these params dynamically to the config.php file?

Secondly, I'm open to experts advise if my implementation is not the best.

Config.php file

<?php 

unset($CFG);
global $CFG;
$CFG = new stdClass();

$CFG->dbtype    = 'mysqli';
$CFG->dblibrary = 'native';
$CFG->dbhost    = 'localhost';
$CFG->dbname    = 'mydb';
$CFG->dbuser    = 'root';
$CFG->dbpass    = '';
$CFG->prefix    = 'my_';
$CFG->dboptions = array (
  'dbpersist' => 0,
  'dbport' => '',
  'dbsocket' => '1',
  'dbcollation' => 'utf8mb4_unicode_ci',
);

$CFG->wwwroot   = 'http://localhost:8888/myapp';
$CFG->dataroot  = '/Applications/MAMP/data/myapp_data';
$CFG->admin     = 'admin';

$CFG->directorypermissions = 0777;

require_once(dirname(__FILE__) . '/lib/setup.php');

This is Moodle. I have tried IOMAD, it is a great app but does not address my need.



Solution 1:[1]

That is a bad solution, IMHO. When you rewrite the configuration file, what happens when the next request comes in that loads that file? They will load the wrong configuration.

I would create two additional configuration files: config_inst_a.php and config_inst_b.php. Then set a session variable when the user logs in that contains the settings file name to load. You can then redefine the database information variables in the additional settings files. If the session variable has a filename in it, load that file AFTER the default config and the database connection values will be replaced.

Added sample code:

Really, really brief:

// Log in user here, and get info about what user company..
$session_start();
$_SESSION['User_ConfigFile'] = 'settings'.$userCompany.'.php';

// More code, or page redirect, or whatever, but the below goes on every page AFTER the default DB is included:

$session_start();
require_once($_SESSION['User_ConfigFile']);

Solution 2:[2]

If it helps somebody, my solution, not in production yet, but for now it works like a charm.

if ($_SERVER['SERVER_NAME'] == 'institutionA.mydomain.com') {
    $CFG->dbname    = 'institutionA';     // database name, eg moodle
    $CFG->dbuser    = 'user_institutionA';   // your database username    

    $CFG->wwwroot   = 'https://institutionA.mydomain.com';    
    $CFG->dataroot  = 'dataroot_institutionA';
    // 
    $CFG->some_custom_data = 'my_institutiona_data';
} else
if ($_SERVER['SERVER_NAME'] == 'institutionB.mydomain.com') {
    $CFG->dbname    = 'institutionB';     // database name, eg moodle
    $CFG->dbuser    = 'user_institutionB';   // your database username    

    $CFG->wwwroot   = 'https://institutionB.mydomain.com';    
    $CFG->dataroot  = 'dataroot_institutionB';
    // 
    $CFG->some_custom_data = 'my_institutionB_data';
} else {
     ...... anything you need in this case
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ouflak