How to migrate cookie domain and retain the value

纵饮孤独 提交于 2020-01-06 07:44:37

问题


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.

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

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

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


回答1:


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);

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.

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.

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



来源:https://stackoverflow.com/questions/50856637/how-to-migrate-cookie-domain-and-retain-the-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!