Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting database dynamically according to sub-domain URL in codeigniter?

I am working on codeigniter site.I have one single application this application is used by various user each user is having its own DB(Its own clients also).I need the way how to approach this cloud system. As i have the single copy of application folder and only the difference in DB for each user. I have tried by creating subdomain directory in codeigniter and writing index file and htaccess file so that i can access my original application.but i need the subdomain path in url and way how to connect to the database according to that subdomian url path.

htaccess file.

RewriteEngine On 
RewriteRule /test/(.*) /$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php

which way i should follow to complete this work.Please help Thanks in advance.

like image 329
Nilesh Solanki Avatar asked May 01 '26 23:05

Nilesh Solanki


1 Answers

Here's what we do:

In config/database.php, we define a set of different DB settings, which are picked based on domain. You can adjust/extend that easily.

if($_SERVER['SERVER_NAME'] == 'www.stagingserver.com'){
  $active_group = "staging";
  $db['staging']['hostname'] = "95.xxx.xxx.xxx";    
} else {
  $active_group = "default";
}

$db['default']['hostname'] = "localhost:8889";
$db['default']['username'] = "root";
$db['default']['password'] = "root";
$db['default']['database'] = "database";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

$db['staging']['username'] = "movers_user";
$db['staging']['password'] = "staging_user";
$db['staging']['database'] = "staging_database";
$db['staging']['dbdriver'] = "mysql";
$db['staging']['dbprefix'] = "";
$db['staging']['pconnect'] = TRUE;
$db['staging']['db_debug'] = TRUE;
$db['staging']['cache_on'] = FALSE;
$db['staging']['cachedir'] = "";
$db['staging']['char_set'] = "utf8";
$db['staging']['dbcollat'] = "utf8_general_ci";
like image 103
Caspar Chiquet Avatar answered May 03 '26 22:05

Caspar Chiquet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!