I am using two database one is default which I would like to set db name as per session value, second database I am using to login and registration and set session once user logged in.

Can anyone have solution to manage this database name after login of user based on session value..

I am using codeigniter framework

Dani AI

Generated

Short answer for : pick the approach that matches your tenancy model.

  • If only the database name changes but hostname/credentials are the same, switch the DB on the existing connection (CI3: $this->db->db_select($name)).
  • If credentials or other connection params vary per tenant, create a per-tenant connection from a config array and use that as the active connection (CI3: $this->load->database($config, TRUE); CI4: \Config\Database::connect($params) or $db->setDatabase($name)). (codeigniter.com)

Practical CI3 pattern (put this in a base controller so every request uses the right DB; do not autoload tenant-dependent models — load them after the DB is set):

// application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
  public function __construct() {
    parent::__construct(); // ensure session library is autoloaded
    $tenant = $this->session->userdata('tenant_id'); // set at login
    if ($tenant) {
      // use your auth DB (group 'auth') to lookup tenant credentials
      $auth = $this->load->database('auth', TRUE);
      $row  = $auth->get_where('tenants', ['id'=>$tenant])->row_array();
      $cfg  = [
        'hostname'=>'localhost','username'=>$row['db_user'],
        'password'=>$row['db_pass'],'database'=>$row['db_name'],
        'dbdriver'=>'mysqli','pconnect'=>FALSE
      ];
      $this->db = $this->load->database($cfg, TRUE); // replace default
    }
  }
}

Do not autoload models that expect the tenant DB — load them after this runs or inject the DB object into them. (codeigniter.com)

CI4 hint: use BaseController::initController to read the session and call \Config\Database::connect($params) (or setDatabase() when only the DB name changes). That ensures all controllers that extend your base get the tenant connection early. (codeigniter4.github.io)

Security / troubleshooting: never store raw DB credentials in the session — store a tenant id and map it to a vetted, server-side config (whitelist names). Regenerate the session on login, use HTTPS/HttpOnly/SameSite, and handle connection failures gracefully. If you see “unknown database” or permission errors, dump the session value and the resolved config, then try connecting manually. Follow OWASP session guidance for safest behavior. (cheatsheetseries.owasp.org)

As suggested, there are multiple ways — this gives the concrete patterns most people use.

Recommended Answers

All 3 Replies

There are priors to this question. Example:

Tried that but no luck

To proceed you'll have to share your code and hope folk will see where the issue is. This has many prior solved discussions with more than a few ways to do this. As such and because your system is your system there is no set piece of code you just slap into place.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.