Reputation: 99
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.
Upvotes: 3
Views: 2630
Reputation: 99
I have tried something like this..... In my database.php file
$active_group = 'default';
$active_record = TRUE;
$uri = $_SERVER['REQUEST_URI'];
$pieces = explode('/', $uri);
if($pieces[1]==""){
$_SESSION['user_db_username']='**';
$_SESSION['user_db_pass']='**';
$_SESSION['user_db_name']='**';
}
else
{
$link = mysql_connect('**', '**', '**');
if($link){
$db_selected = mysql_select_db('**', $link);
if ($db_selected)
{
$query_arr= "SELECT * FROM ** where domain='".$pieces[1]."' ";
$queryResult_arr=mysql_query($query_arr,$link);
while($row=mysql_fetch_assoc($queryResult_arr))
{
$_SESSION['user_db_user']=$row['**'];
$_SESSION['user_db_pass']=$row['**'];
$_SESSION['user_db_name']=$row['**'];
}
}
}
}
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = $_SESSION['user_db_username'];
$db['default']['password'] = $_SESSION['user_db_pass'];
$db['default']['database'] = $_SESSION['user_db_name'];
Upvotes: 2
Reputation: 46
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";
Upvotes: 2