如何将 cookie 发布到不同的链接?
有没有办法使用 javascript 发布特定域(例如 .example.com)可用的所有 cookie(cookie 名称、值和过期时间)? 。我拥有需要从中发布 cookie 的域,但我想将它们发布到不同的域(例如 example2.com)。在 cookie 被 POST 后,我还需要将客户端重定向到特定链接,因此我认为可能需要一些 ajax
注意:我不需要在不同域上读取/写入 cookie。我只需要将当前域的 cookie 名称/值/exp 作为 HTTP POST 值发送/传输到不同的域
Is there any way to POST all the cookies(cookie name , value and expire time) available for a specific domain (e.g .example.com) using javascript ? . I own the domain that I need the cookies to POST from but I want to post them to a different domain (e.g example2.com). After the cookies are POST ed I also need to redirect the client to a specific link so I think some ajax may be required
Note : I do not need to read/write cookies on different domain. I simply need to send/transport the cookies names/values/exp of the current domain to a different domain as HTTP POST values
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
document.cookie
访问 cookie。然而,这只能给你名称和值——据我所知,没有办法获得 cookie 的过期日期。它包含一个包含所有 cookie 的字符串,位于name1=value1; 中。名称2=值2; name3=value3;
格式。可以使用 跨域 XHR 将其作为 POST 请求发送到另一个域,但是如果您不需要读取请求的HTTP响应,提交表单就足够了。只需创建一个不可见的
使用 JavaScript 动态创建
和
You can access the cookies using
document.cookie
. However, this only gives you the name and value - there's no way (that I know of) to get the expiration date of a cookie. It contains a string with all the cookies, in aname1=value1; name2=value2; name3=value3;
format.Sending it as a POST request to another domain can be done with cross-domain XHR, but if you don't need to read the HTTP response of the request, submitting a form should be enough. Simply create an invisible
<form>
with its method attribute set to "post", the action attribute set to the URL on the other domain and the target attribute set to the id of an invisible iframe, add the cookies as an<input>
, and submit the form.Its probably better to create the
<iframe>
and<form>
dynamically, using JavaScript, instead of having it written in the HTML, but I'm too lazy to write that at 2:30AM, sorry :Pnote: If the first domain is accessed on SSL, make sure the connection to the other domain is also over SSL, otherwise you'll be transmitting secured cookies over HTTP as plain text. You can remove the scheme part from the URL of the other domain (e.g.
//www.someotherdomain.com/handle_cookies.php
instead ofhttp://www.someotherdomain.com/handle_cookies.php
), making it use the same scheme as the one used where the cookies are sent from. I highly recommend doing that.该链接描述了一种接近要求的方法。但它使用
window.name
属性而不是cookies
来发送数据。Google 缓存副本,因为原始链接被占用一段时间。
使用 window.name 传输进行跨站点 POST 脚本
The link describes a method that comes close to the requirement. But it uses the
window.name
property instead ofcookies
to send data.Google cache copy, because the original link seized to work for a while.
Using window.name transport for cross-site POST scripting
我认为由于安全原因,您无法读取/写入不同域的 cookie。您可以应用 cookie 可用的特定路径,例如根目录之外的特定文件夹。我认为浏览器的工作方式是找到当前正在访问的网站的 cookie,并相应地使用它们。但允许 cookie 跨域将会带来许多威胁。我想,如果你真的想要的话,我不能保证这样的事情完全有效。
如果您在另一个域上构建一个脚本,该脚本将根据触发器写入一个 cookie,然后使用 PHP cURL 之类的东西将页面带入您当前使用的域中,您可能能够从另一个域调用 cookie领域。这是纯粹的理论,尽管我没有测试过。这个想法是因为您拥有两个域,所以假设您也可以访问两个托管服务器。因此,你需要两端都有一些东西来相互合作来做你想做的事,而不是希望得到一个片面的解决方案。
参考:http://www.quirksmode.org/js/cookies.html
I think due to security reasons you can't read/write a cookie for a different domain. You can apply a specific path for the cookie to be available to such as a specific folder outside of your root. I think the way the browsers work is they find cookies for the site they are accessing at the moment, and use them accordingly. But allowing for cookies to be cross domains would open up to many threats. I think, if you really want though I can't promise something like this working fully.
If you build a script on the other domain that will write a cookie based on a trigger and then you use something like PHP cURL to bring the page into the domain your working with at the moment you may be able to invoke a cookie from the other domain. This is pure theory though not something I have tested. The idea is since you own both domains its assumed you also have access to both hosting servers. So with that you need something on both ends to work with one another to do what you want, rather then hope for a one sided solution.
Reference: http://www.quirksmode.org/js/cookies.html