Cookie Domain in Drupal

There are times when we need to force a specific cookie domain in Drupal. This often comes as a request from the Analytics team as the Google Analytics module uses the $cookie_domain variable in its tracking.

This variable is set in settings.php. Care has to be taken though because setting the cookie domain to be that of the live site will prevent you from logging in on a dev site.
Here is the fix:
Add this line to settings.php

= (strstr($_SERVER['SERVER_NAME'], '') ? '' : '');

The live domain (the false option in the ternary) MUST start with a dot.