I'm working on a multi-domain cookie module for a multilingual site (each language has its own domain) and need some help with the session cookie handoff.

First I tried using the code: htmlspecialchars(SID) to get the session information, but got just a blank string. Using session_name and session_id, I get output, but it's scrambled. The entire module is TINY, so I'm posting it here. Can anyone help me figure out what is wrong with this code?

Also, is there a "drupal approved" way of mini-bootstrapping from my little php image target so the session is opened? I'd rather not just do a raw session_start since drupal could potentially change the session name in some future release.

.module code

<?php
// $Id$

/**
  * Implementation of hook_help().
  */
function multidomain_cookie_help($path, $arg) {
 $output = '';  //declare your output variable
  switch ($path) {
    case "admin/help#multidomain_cookie":
      $output = '<p>'.  t("Ensures that the PHP session is valid across multiple vanity domains that separate multilingual versions of a single site.") .'</p>';
      break;
  }
  return $output;
} // function multidomain_cookie_help()

/**
  * Implementation of hook_perm().
  * No access restrictions required to function
  */
function multidomain_cookie_perm() {
  return array();
} // function multidomain_cookie_perm()

/**
  * Creates a 0x0 blank img pointing to the cookie bridge with the appropriate session parameters added.
  */
function multidomain_cookie_create_link($linkuri) {
	if (!empty($linkuri)) {
		// Not a relative URI
		$url_parts = parse_url($linkuri);
		$hostname = $url_parts['host'];
		if ($hostname != $_SERVER['SERVER_NAME'] /* && !array_key_exists($hostname,$_SESSION['multidomain_cookie_registered'])*/) {
			// Not the same as this host name, and hasn't yet ben registered
			return '<img height="0" width="0" src="' . drupal_get_path('module', 'multidomain_cookie') .'/multidomain_cookie_bridge.php?' . /*htmlspecialchars(SID)*/ session_name() . '=' . session_id() . '" />';
		}
	}
} // function multidomain_cookie_create_link()

/**
 * Implementation of hook_alter_translation_link().
 *
 */
function multidomain_cookie_translation_link_alter(&$links, $path) {
    foreach (array_keys($links) as $langcode) {
	$links[$langcode]['title'] .= multidomain_cookie_create_link($links[$langcode]['language']->domain);
    }
}

multidomain_cookie_bridge.php:

<?php

// Open the session based on the passed data
session_start();	
// Check so we only do this once
$registered = $_SESSION['multidomain_cookie_registered'];
$server_name = $_SERVER['SERVER_NAME'];
// Avoid duplicate values
if (!array_key_exists($server_name,$registered)) {
	$registered[] = $server_name;
	$_SESSION['multidomain_cookie_registered'] = $registered;
}
// Close it quickly to avoid tying up resources
session_commit();

// Output a 1px transparent gif.
$blank_hex = "47 49 46 38 39 61 01 00 01 00 80 00 00 00 00 00 ff ff ff 21 f9 04 01 00 00 00 00 2c 00 00 00 00 01 00 01 00 40 02 01 44 00 3b";

$blank_data = hexdec($blank_hex);

$blank_img = imagecreatefromstring($blank_data);

if ($blank_img !== false) {
    imagegif($blank_img);
    imagedestroy($blank_img);
} else {
    echo 'An error occurred.';
}

Comments

colan’s picture

Did you ever get this working?

The overall issue is summed up over at How do I use cookies across two different domains?. Maybe you're hitting the security barrier?

If we could come up with a module that handles this properly, it would be great. Domain Access does not handle this, unfortunately:

1.4.1 Logging In To Multiple Domains

The Domain Access module allows the creation of domains with different hosts. However, security standards dictate that cookies can only be read from the issuing domain.

As a result, you may configure your site as follows, but when you do so, users cannot be logged through a single sign in.

example.com
one.example.com
myexample.com
thisexample.com

While example.com and one.example.com can share a login cookie, the other two domains cannot read that cookie. This is an internet standard, not a bug.

Note: See the INSTALL.txt for instructions regarding Drupal's default cookie handling.