Can I use localhost as the domain when setting an HTTP cookie?

I think the domain name of a cookie must have exactly two dots (not counting the final dot after the TLD). So .something.localhost is okay, .google.com is okay, but .localhost or google.com is not. But a glance at RFC 2965 suggests that it's more complicated than that... you might want to read that document, especially section 3.3 (and/or its precursor, RFC 2109).


I had similar problem with setting cookies. Make up a domain name and add it to your hosts file as 127.0.0.1. Then run web application on that domain.


I updated the jQuery plugin to not add the domain to the cookie when it is localhost. That solves my problem without touching the hosts file.

var domain = (options.domain && options.domain !== 'localhost') ? '; domain=' + (options.domain) : '';

I'm using Code Ignitor, and setting the domain to an empty string fixed my problem while working on the application on localhost. I believe this is the better solution as everyone in the development team then doesn't need to mess with their hosts files on Windows.

Production domain values can be put in the config.php of Code Ignitor when deployed on a live site.