Persist your session across your desktop and mobile site

This is a small tip for devs who decide to use separate domains for the desktop site and for the mobile site, and who want users to easily switch between both sites without loosing the session.

e.g.:

The trick is to let Drupal set your cookie for the base domain: drupal.org...

Doing this can simply done by changing the $cookie_domain variable in your settings.php file.

However, according to the Mobile Tools issue queue, users struggle with configuring this seemingly simple setup...

The reason is that if you read the documentation in the settings file, you don't learn that your subdomain needs a leading dot So do not forget the leading dot!

e.g.: ".drupal.org" works and "drupal.org" will not work.

However if you work locally and choose to use virtual host names with no dots (e.g. http://myproject), this approach will not work, since you also have to have at least 2 dots in your domain name... So better use http://myproject.local to better reflect real life domain names!

/**
 * Drupal automatically generates a unique session cookie name for each site
 * based on on its full domain name. If you have multiple domains pointing at
 * the same Drupal site, you can either redirect them all to a single domain
 * (see comment in .htaccess), or uncomment the line below and specify their
 * shared base domain. Doing so assures that users remain logged in as they
 * cross between your various domains.
 */
 
$cookie_domain = '.drupal.org';

Luckily the PHP documentation on setCookie() contains more details:


To make the cookie available on all subdomains of example.com (including example.com itself) then you'd set it to '.example.com'. Although some browsers will accept cookies without the initial ., » RFC 2109 requires it to be included. Setting the domain to 'www.example.com' or '.www.example.com' will make the cookie only available in the www subdomain.

If you want to have a scenario where you use different domains like: http://www.drupal.org and http://www.drupal.mobi, then you cannot set a common cookie... And you will need other solutions.

Disclaimer

The practice of persisting sessions between desktop and mobile site has not (yet) been a requirement yet for any mobile projects I have done ... :)