Subdomain For Each User

Hi there,

My client wants me to develop a website, which can give a subdomain for each user.

I want to achieve this using Yii since there are features inside that can be achieved easily by my previous Yii codes, but I am not sure where to start from.

Even better if each user has his own SQL database.

How can I achieve this?

Where should I start?

Please advise =)

Thanks in advance.

Hi,

User means what? different country user OR different rights user OR user within organisation ?

Hi Chandran,

User means customer, the person who makes a registration to make an account in the website.

Give some example… To clarify your requirement…

in case i am registering with your site…

do i need seperate subdomain?

if another user registered

then he need seperate subdomain…

is it like this ?

Yes, that is correct

So far in my opinion the idea is to duplicate the ‘protected’ folder for the files and run certain SQL statement to duplicate the table using php exec or something.

But it doesnt look nice… seems hacky.

Is there a better way for this?

Hi,

By curiousityIs it possible to have 10000 subdomains… if 10000 users registered with your application ?

I have done something very similar with some projects at work.

You make your domain with all subdomains point to the server in DNS. The webserver is configured to receive all requests for that domain, and the same application instance is run for every request (*.domain.com -> /var/www/youapplication/). Then you use apache rewrites to detect if request has "www" or something else as subdomain. If not "www", then rewrite the request to "www.domain.com/r=blog/view&username=%1"

The is not a detailed recipie, but gives you the idea of what to do.

My last project uses one instance of an application for multiple customers. Each customer is separated with its own configuration file, and the index.php file will detect which subdomain is requested, and load the appropriate configuration file.

Ask if something is unclear.

@Chandran: Yes thats what I am looking for

@Paul:

Yes, your answer gives me idea.

But it seems it still uses 1 database.

What I am looking for is 1 user has 1 subdomain which has 1 database (not shared)

I wonder if it is possible in Yii to provide unique database for each subdomain name

I take it you’ll be fully controlling user registration?

Hi there,

Sorry for resurrecting old thread, but I am still unable to find a solution for this.

@Keith: Since I am an admin, yes.

Please advise on where to start in developing this. I am totally clueless =(

If you want a different db for each subdomain (=user) you can do this like below.

But you have to create the db by a script on user registration.

Define a const (for example SUBDOMAIN_DBNAME) with the value of the subdomain in your index.php




...


//extract the subdomain and add a prefix 'subdomain_' to the dbname

$dbname='subdomain_'.array_shift(explode(".",$_SERVER['HTTP_HOST'])); 

define('SUBDOMAIN_DBNAME',$dbname);


...

Yii::createWebApplication($config)->run();



In you config/main.php




 'components' => array(

        'db' => array(

            'connectionString' => 'mysql:host=dbserver1;dbname='.SUBDOMAIN_DBNAME,

            ...

        ),




If your app has shared data between users, you can work with multiple databases.

db -> the global db with shared data

userdb -> the user specific data




 'components' => array(

        'db' => array(

              'class'=>'CDbConnection', 

              'connectionString' => 'mysql:host=dbserver1;dbname=globaldb',

            ...

        ),

        'userdb' => array(

           'class'=>'CDbConnection',  

           'connectionString' => 'mysql:host=dbserver1;dbname='.SUBDOMAIN_DBNAME,

            ...

        ),