3 ms·
Ask HN: How do I store cookies?
The landscape of the web is a bit difficult to research sometimes. I thought maybe you guys can help ...
I want to write a small analytics tool that people can install on their websites. The question is, how can I track the user sessions?
My biggest problem is that safari (under its default setting) refuses to write cookies on any other domain besides the one in the location bar, whether for documents requested by an iframe, or any resource requested by javascript. Chrome allows this for image tags, but Safari does not.
It is discussed here, but a lot of the methods suggested do not work anymore:
http://stackoverflow.com/questions/408582/setting-cross-domain-cookies-in-safari
It would be very useful for an analytics tool to be able to write COOKIES back to the domain where it's hosted, so it can keep track of sessions. But what is a good way to achieve in this ALL browsers?
I have heard of evercookie. Will this work on all the browsers?
If I can't do this, then I guess I will have to mooch off the site's own cookies, which I think is what most analytics tools do. I guess this is designed to prevent tracking a user across multiple domains.
- pseudonym 16y agoI'm curious what you mean by when you say "track user sessions". Are you talking across multiple sites? Pages on the same domain? Or all sites that are running your analytics? You explicitly, for good reason, cannot read and write cookies from another domain. What you can do, however, is host a PHP script that displays as an image on a 3rd party server (http://www.configure-all.com/howto_display_image.php http://www.configure-all.com/howto_display_image.php), and use that script to write cookies or store information on that server's domain.
- EGreg 16y agoLet's say I navigate to publisher.com If publisher.com has an iframe, img or other element with src coming from analytics.com, then (almost) all browsers will allow reading cookies from it. That is how e.g. commenting systems know who you are across domains. However, safari is the only browser that doesn't seem to allow WRITING to a domain other than the one you have navigated to, using any method. (Not even loading img tags.) I was wondering if there was a way around it. Someone said that doing a POST to an iframe used to work http://anantgarg.com/2010/02/18/cross-domain-cookies-in-safari/ http://anantgarg.com/2010/02/18/cross-domain-cookies-in-safa... but now people are saying it doesn't anymore
- pseudonym 16y agoConsidering the number of pages that rely on that php-image style scripting for simple things (like page hits), I'd be surprised if that didn't work on Safari. Sadly I don't have a Mac on hand, so I can't test it myself.
- byoung2 16y agoIt would be very useful for an analytics tool to be able to write COOKIES back to the domain where it's hosted, so it can keep track of sessions If you take Google Analytics as an example, it uses Javascript to gather info on the current visitor, then it creates a dynamic image tag to send that info to Google. This allows them to set a cookie using the domain of the site containing the analytics tag, and send that data back to a server on a different domain. It is also possible to track users across multiple domains (e.g. the DART cookie used by DoubleClick). I believe this is achieved by using a dynamically generated iFrame to set the cookie on the user's computer. Since the domain used for the the iFrame is the same across all sites that use the Javascript snippet, the cookie can be read across multiple sites.
- EGreg 16y agoSo google uses the cookies of the publisher's site, okay... but then what if the publisher has www.something.com, foo.something.com and something.com ... are all of these considered different domains? And if so, isn't it kind of unhelpful to the publisher, that you can't track a session across their various subdomains? Or is this intended?
- EGreg 16y agoAha, I see, I can set a cookie for *.domain.com (or .domain.com) and then it will also work for all subdomains.