如何迁移Cookie域并保留其价值 [英] How to migrate cookie domain and retain the value

查看:88
本文介绍了如何迁移Cookie域并保留其价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从Cookie迁移域?
我有一个跟踪脚本女巫在跟踪脚本的域(服务器端,RoR后端)上设置了一个cookie,可以说mytracking.com。

Is it somehow possible to migrate domains from a cookie? I have a trackingscript witch sets a cookie on the domain of the tracking script(server side, RoR backend) lets say mytracking.com.

跟踪脚本本身作为javascript集成在域mycustomer.com上。

The tracking script itself is integrated as javascript on the domain mycustomer.com.

现状是,网站mycustomer.com上的每个访客现在都有一个用于mytracking.com域的cookie。

Status quo is, every guest on the website mycustomer.com has now a cookie for the domain mytracking.com.

我可以以某种方式将cookie域从mytracking.com迁移到mycustomer.com吗?

Can I somehow migrate the cookie domain from mytracking.com to mycustomer.com?

推荐答案

在客户端JavaScript中,生成唯一的ID。然后创建一个 iframe ,其源指向 mytracking.com 上的脚本,并将唯一ID作为参数。 / p>

In your client side JavaScript, generate a unique ID. Then create an iframe with a source pointing to a script on mytracking.com and the unique ID as a parameter.

var ifrm = document.createElement('iframe');
ifrm.setAttribute('src', 'mytracking.com/storecookie.rb?uuid=<UUID>');
document.body.appendChild(ifrm);

现在 storecookie.rb 脚本可以访问 mytracking.com 域上的cookie值,并将其与您生成的UUID一起写入数据库。

Now the storecookie.rb script can access the cookie value on the mytracking.com domain and writes it to a database along with the UUID that you generated.

现在,在客户端JavaScript中,您 fetch()另一个脚本,例如 mytracking.com/readcookie.rb?uuid=<UUID> ; 从数据库中检索匹配的cookie值。使用客户端JS中的值,您可以简单地创建具有正确域的新cookie。

Now, in your client side JavaScript, you fetch() another script, for example mytracking.com/readcookie.rb?uuid=<UUID> that retrieves the matching cookie value from the database. With the value in your client side JS, you can simply create a new cookie with the correct domain.

不幸的是,该过程有些复杂,但是跨域安全性阻止了为另一个域设置Cookie。

Unfortunately that process is a bit convoluted, but cross-domain security prevents setting a cookie for another domain.

这篇关于如何迁移Cookie域并保留其价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆