Close and Go BackBack to Viget

Rails Sessions Not Working with Safari? A Solution …

Matt Swasey
Matt Swasey, Web Developer, January 23, 2009 4

Having trouble setting session variables in Safari and IE, but not Firefox?  I thought I'd share this quick tip to help save you some time and frustration. 

Bottom line: Don't use underscores in your hostname. Read on for more information.

The Problem

I was working on a rails application locally using passenger -- my development url was http://ux_prototype.local. I had everything working in my development environment so I pushed it out to production for April to check things out. Everything looked great and we were ready to roll.  Or so I thought... 

The app was throwing 500 errors in strange places whenever we were testing in Internet Explorer. 

Using Firefox, I was cruising along with no problems. Checking in Safari  from time to time things looked fine.   In Internet Explorer, things looked ok at first, however, when making a request for an action that altered session data (logging in or out), the 500 error appeared.  Going back to Safari I noticed that, in fact, it was having the same problems as IE.

Neither server-side clearing of session data (ActionController::Base#reset_session) nor client-side deleting the cookie from browser preferences helped at all. Using ./script/server to start Mongrel, however, did help. Could it be the hostname?

The Solution

Having a clearly defined problem revealed better results. As soon as I changed the hostname to http://ux-prototype.local, the problem went away.

So, don't use underscores in your hostname. While underscores are now considered "valid," many DNS clients and browsers will react with varying results. While this may be obvious to some, it created some frustration on my part and could easily be sidestepped in the future by avoiding this situation altogether.

Heres hoping it might save you some time and frustration as well!

Morgan Roderick said on 01/29 at 05:25 AM

Wow, that must have taken awhile to figure out.
Thanks for sharing!

Paul Horsfall said on 02/01 at 10:16 AM

Thanks for the pointer. I just can across something similar moving a site over to using passenger. Safari was happy with an underscore in the host name when ‘Accept cookies’ was set to ‘Always’, but never set the session when it was set to ‘Only from sites you navigate to’. Thanks.

Shane Sizer (sksizer) said on 04/19 at 06:37 PM

wow - huge help - I hope I would of worked to the same answer but this saved a lot of time.

Chris Sund said on 01/22 at 01:39 AM

Would a url have the same affect? www.mysite.com/front_page

Name:

Email:

URL:

Not an infant or robot? Please prove it:
What color is the sky?

Some HTML (strong, a, em) is allowed.

Notify me of follow-up comments?

We're the Developers

at Viget Labs. We write about web development trends, tips, best practices, industry events, and our projects — all with an emphasis on Ruby on Rails.

Recent Comments

Ah, that’s neat.  I hadn’t heard of Typus before.  I played around with Hobo and some others a couple of years ago but they all fell flat with me for some reason.

Contact Us

Have any questions, comments, ideas, or secrets to share? Let us know.


What color is the sky?

Sorry, you need to have Javascript enabled to use this form. (Don't blame us, blame the spammers!) If you'd like to contact us, please visit our Contact page.