3

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.

5
  • there is nothing to do with .htaccess, instead you have to apply your logic in route.php Commented May 13, 2013 at 4:56
  • how can i work with route.php?Please suggest any artical or any example to read... Commented May 13, 2013 at 5:18
  • read this article : net.tutsplus.com/tutorials/php/… Commented May 13, 2013 at 5:20
  • for .htaccess : stackoverflow.com/questions/7085670/… Commented May 13, 2013 at 5:21
  • in that artical he have diffrent controller for each subdomain, but i have only one application i have to just share taht codebase and have to connect with diffrent DB every time according to subdomian url.... Commented May 13, 2013 at 5:30

2 Answers 2

2

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";
Sign up to request clarification or add additional context in comments.

2 Comments

I am going by directory model..I have done with proper redircting of my directory to the main application but now i am little bit confuse about database connection for each directory...
I think you can still use the above method. CI will use the DB settings of your database.php in the application folder of your respective application, while using the shared system folder.
2

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'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.