How I can set third party cookie

15,531

Solution 1

It would be possible to directly share cookies if instead of abc.com, cde.com, def.com, you would have abc.xyz.com, cde.xyz.com, def.xyz.com, (google for subdomain cookies). Maybe it is possible to set your websites like that and still meet your requirements.

Otherwise, if all of those websites cannot be in subdomains of the same domain, then you may have one of them act as a central cookie server and when the user is on other domains you could use JSONP to direct them to some script on your cookie domain that would send you their id or whatever and make your script that handles the AJAX request set its domain cookie to the same value. Example:

  1. user visits def.com
  2. JavaScript code on def.com makes an JSONP request to abc.com
  3. abc.com sets a cookie if it is not set yet
  4. abc.com returns the cookie value as a response to the script on def.com
  5. script on def.com sets its local def.com cookie to the same value

and now your servers can coordinate their statistics etc.

All of this is of course possible only if all of the websites cooperate with each other, ie. your websites cannot mess with cookies of other websites that you don't control as well.

UPDATE:

See also Breaking The Cross Domain Barrier talk by Alex Sexton for some inspirations and code example.

UPDATE:

If you decide to use a method similar to the one outlined above, make sure you understand the potential security issues like a possibility of a cross site request forgery attack. Search Stack Overflow for JSONP security to find more informations on how to make it safe. Keep in mind that the above explanation is a simplification of a somewhat complicated process that you need to understand. You have been warned.

Solution 2

You can only access your domain cookies.

Solution 3

It is not possible to assign multi-domain cookies.

Share:
15,531
Alok
Author by

Alok

Updated on September 23, 2022

Comments

  • Alok
    Alok over 1 year

    How I can set third party cookie. I am having requirement set cookie and cookie will be enable d in visited websites, Like I have set cookie in abc.com when I visit cde.com or def.com or ghi.com so set cookie will be fetch on all the website. How I can fetch cookie on all domain in javascript.