Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Sign In
Not a member yet? Click here to register.

Multi site with one installation

by , Last updated on 3 months ago in Code Snippet and functions - 8
I made a multi subdomain site with 1 PHP-Fusion installation, which uses a separate database for each site.

It is V7.03, but can also be V8.xx.

I have multi subdomains like:
www.sub1.mysite.com
www.sub2.mysite.com

On the webserver every subdomain is pointing to the same folder on the webserver.

each site has its own database e.g. databasenames are like mysite_sub1 and mysite_sub2
each site has its own config.php in the root of the site e.g. config_sub1.php and config_sub2.php

In each config.php the settings for the database access are specified for each subdomain.
So in config_sub1.php the name of the database is: mysite_sub1, together with database login credentials, and different cookie prefix to prevent messing up with different login sessions.

In maincore.php I added the following:

function StripSubdomain($Domain) {
 $domain_array = explode('.', $Domain);
 $domain_array = array_reverse($domain_array);
   return $domain_array[2];
}

$subdomain = StripSubdomain($_SERVER['HTTP_HOST']);
define("SUBDOMAIN", $subdomain);
define("CONFIGPHP", "config_".SUBDOMAIN.".php");



and I modified:

// Locate config.php and set the basedir path
$folder_level = "";
$i = 0;
while (!file_exists($folder_level."config.php")) {
   $folder_level .= "../";
   $i++;
   if ($i == 7) {
      die("config.php file not found");
   }
}


to

// Locate config.php and set the basedir path
$folder_level = "";
$i = 0;
while (!file_exists($folder_level.CONFIGPHP)) {
   $folder_level .= "../";
   $i++;
   if ($i == 7) {
      die("config.php file not found");
   }
}



and

require_once BASEDIR."config.php";
// If config.php is empty, activate setup.php script
if (!isset($db_name)) {
   redirect("setup.php");
}


to

require_once BASEDIR.CONFIGPHP;
// If config.php is empty, activate setup.php script
if (!isset($db_name)) {
   redirect("setup.php");
}


I Tested and this is working OK for me.
So now I can run two different sites from one installation, only different databases.

Question is:
Is this a good practice to do?
It probably need some code to prevent $_SERVER['HTTP_HOST'] containing wrong domain name data.

One thing more to do is: there are some cron-jobs executed also, this needs some modification too, to be sure the cron job is executed with the right database.
This can be done by adding a parameter to the cron-job command line, so the con-job php file 'knows' which database to open, or modify the cron-job php file to run through all the databases after each other.
You can view all discussion threads in this forum.
You can start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You can download attachments in this forum.
Users who participated in discussion: douwe_yntema