最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - how to get browser stored cookies using javascript - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Sep 7, 2011 at 14:15 somu.websomu.web 3871 gold badge5 silver badges14 bronze badges 1
  • 1 Shell script to access browser cookies? a Typo? – ajreal Commented Sep 7, 2011 at 14:17
Add a ment  | 

5 Answers 5

Reset to default 3

You can access them using

document.cookies

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 mand. 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.

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.

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 pare 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 listCookies() {
var theCookies = document.cookie.split(';');
var aString = '';
for (var i = 1 ; i <= theCookies.length; i++) {
    aString += i + ' ' + theCookies[i-1] + "\n";
}
return aString;
}
发布评论

评论列表(0)

  1. 暂无评论