如何使用javascript获取浏览器存储的cookie

发布于 2024-12-03 09:36:37 字数 107 浏览 0 评论 0原文

我对使用 javascript 访问所有浏览器 cookie 有疑问,我能够在 shell 脚本中执行此操作,但我想访问存储在本地计算机中需要传递到服务器的 cookie 信息。

问候

I have question regarding accessing all browser cookies using javascript, i was able to do this in shell script but i want to access the cookie information stored in local machine which needs to pass to server.

Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

回忆躺在深渊里 2024-12-10 09:36:37

您可以使用以下方式访问它们

document.cookies

You can access them using

document.cookies
辞旧 2024-12-10 09:36:37

您无法访问所有浏览器cookie。仅为当前域设置 cookie,并且未标记为“仅限 http”(如果您位于非 SSL 页面,则为“安全”)。

在 javascript 中使用 document.cookies

更新
浏览器具有内置功能(作为安全功能),可防止您读取跨域 cookie。只要 javascript 在浏览器中运行,就没有方法访问这些 cookie,更不用说执行 shell 命令了。 Google 搜索:同源政策

您基本上正在寻找的内容具有如此多的安全/隐私影响,我什至不知道从哪里开始解释危险。

You cannot access all browser cookies. Only cookies set for the current domain and not marked 'http-only' (or 'secure' if you are on a non-SSL page).

In javascript use document.cookies

Update
The browser has built in functionality, as a security feature, to prevent you from reading cross domain cookies. As long as the javascript runs in the browser, there is no method to access those cookies, let alone execute a shell command. Google for: same origin policy.

What you basically are looking for has so many security/privacy implications, I don't even know where to start explaining the dangers.

遮了一弯 2024-12-10 09:36:37

想象一下这是可能的。您浏览到加载第三方广告的任意网站,流氓广告会读取您所有的浏览器 cookie,瞧!俄罗斯黑手党的某个人拥有您所有网站的“记住我”cookie 和会话 ID。他可以阅读您的电子邮件、在 Facebook 上查看您的照片并从您的 PayPal 帐户中取回资金。

Imagine that was possible. You browse to an arbitrary site that loads third-party ads, a rogue ad reads all your browser cookies and, voilá!, some guy from the Russian Mafia has the "Remember me" cookies and session IDs for all your sites. He can read your e-mail, see your pics on Facebook and retrieve money from your PayPal account.

难理解 2024-12-10 09:36:37
function getCookie(name) {
  // Split cookie string and get all individual name=value pairs in an array
  var cookieArr = document.cookie.split(";");
  // Loop through the array elements
  for (var i = 0; i < cookieArr.length; i++) {
    var cookiePair = cookieArr[i].split("=");
    /* Removing whitespace at the beginning of the cookie name
        and compare it with the given string */
    if (name == cookiePair[0].trim()) {
      // Decode the cookie value and return
      return decodeURIComponent(cookiePair[1]);
    }
  }
  // Return null if not found
  return null;
}
function getCookie(name) {
  // Split cookie string and get all individual name=value pairs in an array
  var cookieArr = document.cookie.split(";");
  // Loop through the array elements
  for (var i = 0; i < cookieArr.length; i++) {
    var cookiePair = cookieArr[i].split("=");
    /* Removing whitespace at the beginning of the cookie name
        and compare it with the given string */
    if (name == cookiePair[0].trim()) {
      // Decode the cookie value and return
      return decodeURIComponent(cookiePair[1]);
    }
  }
  // Return null if not found
  return null;
}
把时间冻结 2024-12-10 09:36:37
function listCookies() {
var theCookies = document.cookie.split(';');
var aString = '';
for (var i = 1 ; i <= theCookies.length; i++) {
    aString += i + ' ' + theCookies[i-1] + "\n";
}
return aString;
}
function listCookies() {
var theCookies = document.cookie.split(';');
var aString = '';
for (var i = 1 ; i <= theCookies.length; i++) {
    aString += i + ' ' + theCookies[i-1] + "\n";
}
return aString;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文