[DEV] Multiple domains/sites access the same database

MOD Authors: Discuss and post updates on new MODs in development for phpBB3, Receive feedback and Feature requests for MODs in development.

To submit your MOD or style, go to the STG MOD Manager
Forum rules
go to the STG MOD Manager to post your MOD in development.
Discuss and receive feedback for any MOD in development for phpBB3.
Suggest features for MODs in development.

No Support or MOD Requests
-- exceptions for MODs only posted here as Beta or Alpha.
Support requests for a MOD should be requested in the respective MOD topic.

[DEV] Multiple domains/sites access the same database

Postby Highway of Life » 06 Feb 2007, 18:30

Transferring the STG website to a new Server, and waiting for the insanely slow DNS entries to properly propagate has enlightened me to the cookie system with phpBB3, and how, by default, it really does not work well with more than one domain at a time.

So, I'm going to create a MOD where it will be able to work with more than one domain at the same time, and access the same database...

http://startrekaccess.com and http://startrekguide.com will be my testing bed, since both are using the same database, but they are different domains.
I plan on making this first thing Friday Morning.
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10


Re: [DEV] Multiple domains/sites access the same database

Postby andrewbelcher » 06 Feb 2007, 18:57

This could be done using the $_SERVER predefined variables, and then assigning the cookie domain to use that domain. This is an adaptation from the domain controlling that we (yautja_Cetanu and I) are using for our 'TheVR' project (I'll include what we are using in the spoiler section below, as that also sets up some other stuff, which you may be interested in)... This gets inserted at the end of common.php.

Code: Select all
    $config['cookie_domain'] = $_SERVER['SERVER_NAME']; // Get the domain information out of the server url (eg www.thevr.co.uk) and assign it to the cookie domain;

Spoiler:
This does more - for example grabs the file name etc and also gets site-specific information from a table of sites on the database. This is how we're handling multiple sites (with multiple domains) off of one phpBB3 install.
Code: Select all
/***** START
ADDED by andrewbelcher for thevr.co.uk
This processes the URL so the correct site settings can be used.
*****/

    
$domain $_SERVER['SERVER_NAME']; // Get the domain information out of the server url (eg startrekguide.com)

    
$pieces explode('/'$_SERVER['REQUEST_URI']); // Split up the script filename (including path) by '/'
    
$file $pieces[count($pieces) -1];    // Get the filename (last piece from above)

    
$i 0;
    
$path '';
    while (
$i count($pieces) -1// Generate the path
    
{
        
$path .= $pieces[$i] . '/';
        
$i++;
    }

    
$vars = array();
    if (
strpos($file'?') == true)
    {
        list(
$filename$variables) = explode('?'$file); // Split the filename into the script and the variables

        
$temp = array();
        if (
$variables <> '')
        {
            foreach (
explode('&'$variables) as $k=>$v// Seperate the variables into an array (Name => Value)
            
{
                
$temp explode('='$v);
                
$vars[$temp[0]] = (isset($temp[1])) ? $temp[1]: '';
                }
        }
    }
    else
    {
        
$filename $file;
    }

    
// Check this against the database
    
$sql 'SELECT s.* , su.domain
        FROM ' 
SITES_TABLE ' s INNER JOIN ' SITE_URL_TABLE " su ON s.site_id = su.site_id
        WHERE su.domain = '$domain'
        LIMIT 1"
;
    
$result $db->sql_query($sql);
    
$row $db->sql_fetchrow($result);

    if (empty(
$row['site_id']))
    {
        
$config['site_id'] = -1;
        
$config['site_guest_id'] = ANONYMOUS;
        
$config['cookie_domain'] = $domain;
    }
    else
    {
        
// Store the information in a config array $config
        
$config array_merge($config, array(
            
'site_id'            => $row['site_id'],
            
'site_name'            => $row['name'],
            
'site_guest_id'        => $row['guest_id'],
            
'site_home'            => $row['home'],
            
'site_domain'        => $domain,
            
'site_file'            => $filename,
            
'site_vars'            => $vars)
            );
    }
Hope that helps! It should work fine as no cookies have been handled by that point...
Last edited by Highway of Life on 06 Feb 2007, 19:14, edited 2 times in total.
Reason: Things look so much better in colour. :D
andrewbelcher
Cadet I
Cadet I
 
Posts: 18
Joined: 14 Jan 2007, 10:40
Gender: Male

Re: [DEV] Multiple domains/sites access the same database

Postby Highway of Life » 06 Feb 2007, 18:59

Okay, first, here is a bug report regarding this issue: http://www.phpbb.com/bugs/viewreport.php?b=7904

And the code fix:
Spoiler:
Code: Select all
*** startrekguide.com/phpBB3/includes/session.php    Sun Jan 28 11:08:13 2007
--- startrekguide.com/forum/includes/session.php     Tue Feb  6 18:45:31 2007
***************
*** 
804,810 ****
 
                
$name_data rawurlencode($config['cookie_name'] . '_' $name) . '=' rawurlencode($cookiedata);
                
$expire gmdate('D, d-M-Y H:i:s \\G\\M\\T'$cookietime);
!               
$domain = (!$config['cookie_domain'] || $config['cookie_domain'] == 'localhost' || $config['cookie_domain'] == '127.0.0.1') ? '' '; domain=' $config['cookie_domain'];
 
                
header('Set-Cookie: ' $name_data '; expires=' $expire '; path=' $config['cookie_path'] . $domain . ((!$config['cookie_secure']) ? '' '; secure') . '; HttpOnly'false);
        }
--- 
804,810 ----
 
                
$name_data rawurlencode($config['cookie_name'] . '_' $name) . '=' rawurlencode($cookiedata);
                
$expire gmdate('D, d-M-Y H:i:s \\G\\M\\T'$cookietime);
!               
$domain = (!$config['cookie_domain'] || $_SERVER['HTTP_HOST'] != $config['cookie_domain'] || $config['cookie_domain'] == 'localhost' || $config['cookie_domain'] == '127.0.0.1') ? '; domain=' $_SERVER['HTTP_HOST'] : '; domain=' $config['cookie_domain'];
 
                
header('Set-Cookie: ' $name_data '; expires=' $expire '; path=' $config['cookie_path'] . $domain . ((!$config['cookie_secure']) ? '' '; secure') . '; HttpOnly'false);
        }


My next version will make it so that it will set cookie(s) for two domains at the same time.
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

Re: [DEV] Multiple domains/sites access the same database

Postby Handyman » 06 Feb 2007, 19:01

If you guys want to see how it works, visit these 3 websites and login.
If it works, you will remain logged in on each one.
http://startrekguide.com/forum
http://startrekaccess.com/forum
http://starwarsaccess.com/forum
http://startrekencyclopedia.com/forum

we'll work on it so if you log into one, it logs you into the others.

Also Highway, we should be able to get it to log you into more than 2 at once Smile (I'm feeling optimistic because in a couple days I will have another domain that we can test it on for a total of 4)
Last edited by Handyman on 07 Feb 2007, 09:47, edited 1 time in total.
Reason: That day is here already? I added the 4th domain.
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7456
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: [DEV] Multiple domains/sites access the same database

Postby andrewbelcher » 06 Feb 2007, 19:03

Ah, ok... Yeh - that makes sense...

Thanks! You made me think about something I'd forgotten!!

My solution will work if you're not wanting the cookies to work across domains, but yeah, if you want it to work seamlessly between them, then that's going to require both domains being set.
andrewbelcher
Cadet I
Cadet I
 
Posts: 18
Joined: 14 Jan 2007, 10:40
Gender: Male

Re: [DEV] Multiple domains/sites access the same database

Postby Handyman » 07 Feb 2007, 09:46

There, just added a 4th Smile
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7456
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: [DEV] Multiple domains/sites access the same database

Postby Highway of Life » 07 Feb 2007, 11:45

Acyd Burn wrote:There should be no problems because on installation a unique cookie name is given. If the users install normally there are no problems.

Acyd Burn wrote:Oh, seeing the two domains on *same* database... i think this is not supported and requires a mod. Smile The change you propose is a cheap hack - nothing should directly overwrite or force a cookie domain, this can give us a lot of support headaches.
Oh well... worth a shot. Smile

So if you want it... it's a MOD.
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

Re: [DEV] Multiple domains/sites access the same database

Postby l3it3r » 07 Feb 2007, 20:37

I did this many times with phpBB2, and was looking into it when I started my PphpBB3 current site, but decided against it. I was able to get it running, but didn't log the changes made.
l3it3r    
MOD Author
MOD Author
 
Posts: 118
Joined: 14 Dec 2006, 15:20
Location: Fairfax County, VA
Favorite Team: Ravens
Gender: Male

Re: [DEV] Multiple domains/sites access the same database

Postby yautja_Cetanu » 26 Feb 2007, 08:44

I'm guessing for your mod Highway you're assuming that both domains have the same admins right? So there is no change really to the admin panel?
yautja_Cetanu    
MOD Author
MOD Author
 
Posts: 66
Joined: 13 Jan 2007, 13:04
Gender: Male

Re: [DEV] Multiple domains/sites access the same database

Postby Highway of Life » 26 Feb 2007, 11:50

Right, again... the idea is to use the same database, but just different websites on the same server.
Since it would be insane to run the websites from separate servers.
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

Next

Return to MODs in Development

Who is online

Users browsing this forum: Google [Bot] and 11 guests