Created
February 15, 2009 22:12
-
-
Save bschwartz/64874 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adjust sessions so they work across subdomains | |
# This also will work if your app runs on different TLDs | |
# from: http://szeryf.wordpress.com/2008/01/21/cookie-handling-in-multi-domain-applications-in-ruby-on-rails/ | |
# modified to work with Rails 2.3.0 | |
module ActionControllerExtensions | |
def self.included(base) | |
base::Dispatcher.send :include, DispatcherExtensions | |
end | |
module DispatcherExtensions | |
def self.included(base) | |
base.send :before_dispatch, :set_session_domain | |
end | |
def set_session_domain | |
if @env['HTTP_HOST'] | |
# remove the port if there is one | |
domain = @env['HTTP_HOST'].gsub(/:\d+$/, '') | |
# turn "brendan.app.com" to ".app.com" | |
# and turn "app.com" to ".app.com" | |
if domain.match(/([^.]+\.[^.]+)$/) | |
domain = '.' + $1 | |
end | |
@env['rack.session.options'] = @env['rack.session.options'].merge(:domain => domain) | |
end | |
end | |
end | |
end | |
ActionController.send :include, ActionControllerExtensions |
Note that this will no work for many domains, as 'google.co.uk' would become 'co.uk', which is not legal.
@findchris, great point and something that actually bit me just the other day. It's a tricky problem to solve in a truly correct and generalized fashion. I ended up just solving my specific problem (not a general solution).
Let me know if you come up with a solution to this. I'd love to see it!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great!