Thursday, April 08, 2010

Drupal 6 multi-site single sign-on

I've just spent the last two days looking into setting up single sign-on across multiple sites under a shared domain. I looked at both the Multisite Login and Single sign on modules but in the end the solution was achieved without either of these modules. Multisite Login correctly notes in the README : "if you are using several sites on the same domain then you do not need this module. The multisite functionality in Drupal core has the ability to share logins for sites like these without additional modules." The problem was there doesn't seem to be much documentation about achieving this. In the end the solution was on Eric's Drupal Blog but it was sort of lost in the detail so I want to note the key elements here.

For this example we have three sites http://www.example.com/master, http://www.example.com/slave1, http://www.example.com/slave2. I want to be able to sign on to one site and to be signed in to all.

Start by setting up the master site as you normally would (if you haven't set up a multisite before this earlier post may help). The only additional thing to do is to set the cookie_domain variable in the sites settings.php:

$cookie_domain = 'master';

Now go ahead and setup slave1. Once again add the cookie_domian (using the same value as the master site) but this time also define the db_prefix array. You use this to tell this site to use the database from the master site for some of the tables. Lets say that the master site uses a database called masterdb. To tell slave1 to use this database for the users table add 'users' => 'masterdb.' to the db_prefix array. The exact list of tables you'll need to add will depend on how much functionality needs to be shared across your sites. Eric's post adds users, sessions and authmap. I've also added filters, filter_formats and role :

$db_prefix = array(
'default' => '',
'access' => 'masterdb.',
'authmap' => 'masterdb.',
'filters' => 'masterdb.',
'filter_formats' => 'masterdb.',
'role' => 'masterdb.',
'sessions' => 'masterdb.',
'users' => 'masterdb.'
);


Now you are ready to install slave1. When that's done you should be able to log out of and into either site and stay logged in across both. Repeat the steps used for slave1 on the slave2 site. That's all there is to it. In essence all that's required is to share some core tables and the cookie domain across all sites. The only problem I struck was if the cookie_domain isn't an actual domain.

1 comment:

Daniel said...

Thanks for your post!

I have an Apache-config question, as I'm trying to get Drupal multisite setup.

I've tried to setup my Apache virtual hosts to allow for your sub-folder style of 'master', 'slave1' and 'slave2'. I'm able to setup the 'master' just fine, but when I try to reach 'slave1' to do the initial installation, it says it's not found. I've edited my httpd-vhosts.conf (XAMPP installation) and my Windows "hosts" file.

I know the files are being read properly, because I can change 'localhost' to something like 'blah' and have it work properly, but only for folders that exist (not for a vhost that points to a folder that exists, like 'slave1' would).

Could you provide any help? Thanks again for your informative articles.