views:

464

answers:

4

Hi, I have user preference (colour, locale settings) on example.com that I would like to migrate to example2.com

I'm wondering if I can use AJAX or a hidden iFrame to pass the cookie information from example.com to example2.com which will then set another cookie with the same information on example2.com.

I know I could do this easily through the URL string and a redirect, but I would like to achieve this in the background if possible -- no redirects -- and needs to work on major browsers (IE6+, FF1.5+, Safari, Opera)

Point is this cookie information isn't secure so there's no risk there.

+2  A: 

It depends on the browser setup for starters, if the browser is configured to reject 3rd party cookies you're out of luck.

Assuming that you control example.com, and the cookie is not an HTTP only cookie you could add a script. The script would write out an img tag pointing to a script on example2.com, with document.cookies as the parameter. Within the script write out a 1x1 transparent gif and attach the cookie from example to the response, having parsed the parameter string to pull out the cookie name/value pairs.

blowdart
stunnaman
They may, if they're configured to allow 3rd party cookies. Most are by default (although IE needs a p3p policy).The script does not return script, it returns the correct binary GIF, but also adds the cookie headers for its own cookie.
blowdart
+1  A: 

Setup a path on example.com that generates a javascript file containing the cookies passed in. Lets say it is example.com/get_cookie.js

Then you can do an ajax call from example2.com to example.com/get_cookie.js to get those cookies, and save them under example2.com.

Since it is cross-domain, you can't use XHR (XmlHttpRequest), instead you'll have append get_cookie.js as a javascript node, and that javascript file will have to call a callback to pass you the data.

So get_cookie.js would look somthing like:

return_data( 'here is my example.com cookie info' );
stevewedig
great idea - thanks
stunnaman
A: 

You would need to make a server-to-server XMLHTTP request, and have the second server recreate the cookies. You can't "pass" them.

Diodeus
A: 

Have you considered using Flash Cookies to store those preferences? The work with all the major browsers include chrome.

http://www.ghacks.net/2007/05/04/flash-cookies-explained/

phill