如何将 cookie 发布到不同的链接?

发布于 2024-11-30 17:05:45 字数 268 浏览 1 评论 0原文

有没有办法使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

跨年 2024-12-07 17:05:45

您可以使用 document.cookie 访问 cookie。然而,这只能给你名称和值——据我所知,没有办法获得 cookie 的过期日期。它包含一个包含所有 cookie 的字符串,位于 name1=value1; 中。名称2=值2; name3=value3; 格式。

可以使用 跨域 XHR 将其作为 POST 请求发送到另一个域,但是如果您不需要读取请求的HTTP响应,提交表单就足够了。只需创建一个不可见的

,其 method 属性设置为“post”,action 属性设置为其他域上的 URL,target 属性设置为不可见 iframe 的 id,添加将 cookies 作为 ,然后提交表单。

<iframe id="foo" style="display: none"></iframe>
<form id="bar" method="post" target="foo"
      action="http://www.someotherdomain.com/handle_cookies.php">
    <input id="cookies" type="hidden" name="cookies" />
</form>
<script type="text/javascript">
    document.getElementById('cookies').value = document.cookie;
    document.getElementById('bar').submit();
</script>

使用 JavaScript 动态创建

可能比用 HTML 编写更好,但我太懒了请在凌晨 2:30 写下,抱歉 :P

注意:如果通过 SSL 访问第一个域,确保到另一个域的连接也是通过 SSL 进行的,否则您'将通过 HTTP 以普通方式传输安全 cookie 文本。您可以从其他域的 URL 中删除方案部分(例如 //www.someotherdomain.com/handle_cookies.php 而不是 http://www.someotherdomain.com/handle_cookies。 php),使其使用与发送 cookie 时使用的方案相同的方案。我强烈建议这样做。

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 a name1=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.

<iframe id="foo" style="display: none"></iframe>
<form id="bar" method="post" target="foo"
      action="http://www.someotherdomain.com/handle_cookies.php">
    <input id="cookies" type="hidden" name="cookies" />
</form>
<script type="text/javascript">
    document.getElementById('cookies').value = document.cookie;
    document.getElementById('bar').submit();
</script>

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 :P

note: 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 of http://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.

南七夏 2024-12-07 17:05:45

该链接描述了一种接近要求的方法。但它使用 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 of cookies 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

无人问我粥可暖 2024-12-07 17:05:45

我认为由于安全原因,您无法读取/写入不同域的 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文