简体   繁体   中英

AngularJs: How to set same cookie on different domains

I have 2 domains :

  1. www.site1.com
  2. www.site2.com

Important Notes:
not sub domain!
Allow-Origin are enable on both domains.
have full Access to both of domains.

Question:

How to set a cookie on site1 & get that cookie on site2 ?

I want to use it on AngularJs, no matter if using jQuery in your examples.

There is a technique that multi-site companies like google employ to keep their users logged in for all their sites per single authentication.

This question is about how A can read a cookie of B . But my answer only tells you how A can set a cookie in B . Although this technique can be employed in a useful way for OPs favor, negative points are welcome. I will still spread the awareness.

Create a php file (lets say setcookie.php) in site B. This can set a cookie for site B.

setcookie("MyCookie", "subinsb.com", time()+3600);

Now if you can call this php file from any site, it will set the cookie for site B. A famous way to call this script is via a hidden img tag. So, the site A can have this image tag - which will set a cookie for site B.

<img src="http://www.siteB.com/setcookie.php" style="display:none;" />

When this image is loaded, you know the cookie for site B is set.

Interestingly, you can send data too to the cookie of site B through the URL. Your setcookie.php can read data via $_GET and include them in the cookie.

Here is the article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM