DISQUS

DISQUS Hello! The Puerto Rican Rails Dude is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

Jump to original thread »
Author

Quick and Dirty Browser Cookie Testing

Started by dennmart · 8 months ago

On a recent Rails project, I was asked to verify if the visiting user’s browser has cookies enabled, and display a message on top of the screen if they don’t. While I don’t want to get into the reasoning why, or get into a flame war whether the site should s ... Continue reading »

3 comments

  • Appreciate the link, but if you'd read my article, you'd have seen that my method does not in fact use a GET parameter. The first thing I found in google did, but I also ditched that, not because it wasn't clean (which it isn't) but because of other bugs it introduces, which I also mentioned in my article.

    That said, I like your solution even more, since while it does run on each page load, the impact should be negligible, and it doesn't require a page redirect at all. Nice work. :)

    -Eric
  • Thanks for your code, Dennis. It works well to detect if cookies can be accepted by browsers, but does not necessarily detect if Safari's "Private Browsing" mode has been enabled. In this mode, new cookies can apparently be created but cookies created outside of "Private Browsing" mode will not be overwritten. This can create almost as many problems for cookie-based sessions as if cookies were not accepted at all.

    I've modified your solution to also test for this case. In order to do this, I set the test cookie to a dynamic value, such as the current time. I then check whether the cookie has been set to the dynamic cookie value to ensure that "private browsing" has not been enabled.

    In order for this to work, you unfortunately can't delete the cookie after the test. Otherwise, the test won't recognize when the user had a previous session created outside of private browsing mode that they are now trying to change in private browsing mode.

    NOTE: I tried to post this comment earlier with a bit of javascript source in my comments, but DISQUS wasn't happy so I trimmed my code out.
  • you are on my rss reader now

Add New Comment

Returning? Login