Multiple local Rails apps and their sessions

If you are working on multiple Rails apps at a time, and run them all on localhost with different ports, watch out for session issues as the default _session_id cookie is shared between the apps. This is because the browser only uses the host name, not the host and port combination for looking up cookies.

What this means is, if you set some session state in one app, and then set some session state in a second app, the first app’s session will be orphaned and your first app’s session will be gone when you try to retrieve it.

There are two ways to have apps not interfere with each other:

Set each app to use a different cookie name.
In your environment.rb file:
ActionController::Base.session_options[:session_key] = 'appname_session_id'
Use host mappings in /etc/hosts

In your /etc/hosts file, add a mapping for a host to your localhost IP address and use that in the browser. e.g,

/etc/hosts
127.0.0.1        my-dummy-server.com

In the browser, your reference this app as http://my-dummy-server.com:port-number. You still need to use a different port, but now the browser thinks it’s a different server and the cookies won’t be shared between the apps.

Interestingly, Windows has an /etc/hosts file in \windows\system32\drivers\etc, so this trick works there too!

This entry was posted on Thu, 26 Oct 2006 01:05:13 GMT . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.
Tags


Trackbacks

Use the following link to trackback from your own site:
http://blog.craz8.com/trackbacks?article_id=multiple-local-rails-apps-and-their-sessions&day=25&month=10&year=2006

Comments

Leave a response

Leave a comment