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

obtain session Id from cookies using javascript - Stack Overflow

programmeradmin6浏览0评论

I am trying to learn about cookies and what all they store. Since I am an active user of Google Chrome, I was thinking of accessing the session Id of a webpage from the cookie using only Javascript. I have obtained the list of all cookies stored in the browser using chrome.cookies.getAll().

However I can't understand how to access the session Id from the cookies since there is no keyword for it ?

How can I obtain the session ID from the cookies?

The code from obtaining the cookies is below :

<html><head>
<script>
chrome.browserAction.onClicked.addListener(getCookies);

var cacheMap = {};
var cookie_nameArr = [];
var cookie_valArr = [];


    function getCookies(){
        chrome.cookies.getAll({}, function(cookies){

            for(var b in cookies){                      
            var cookieVal = cookies[b].value;
            var cookieName = cookies[b].domain;
                    cookie_nameArr.push(cookieName);
                    cookie_valArr.push(cookieVal);              
                if(!cacheMap[cookieName])
                {
                    cacheMap[cookieName] = 1;
                }
                else
                    cacheMap[cookieName]++;
    }//alert(cookie_nameArr.length + "," + cookie_valArr.length);
        for(var i=0;i<3;i++)
    {   
        alert(cookie_nameArr[i] + ", " + cookie_valArr[i]);

    }

});
}

</script></head>
</body></html>

I am trying to learn about cookies and what all they store. Since I am an active user of Google Chrome, I was thinking of accessing the session Id of a webpage from the cookie using only Javascript. I have obtained the list of all cookies stored in the browser using chrome.cookies.getAll().

However I can't understand how to access the session Id from the cookies since there is no keyword for it ?

How can I obtain the session ID from the cookies?

The code from obtaining the cookies is below :

<html><head>
<script>
chrome.browserAction.onClicked.addListener(getCookies);

var cacheMap = {};
var cookie_nameArr = [];
var cookie_valArr = [];


    function getCookies(){
        chrome.cookies.getAll({}, function(cookies){

            for(var b in cookies){                      
            var cookieVal = cookies[b].value;
            var cookieName = cookies[b].domain;
                    cookie_nameArr.push(cookieName);
                    cookie_valArr.push(cookieVal);              
                if(!cacheMap[cookieName])
                {
                    cacheMap[cookieName] = 1;
                }
                else
                    cacheMap[cookieName]++;
    }//alert(cookie_nameArr.length + "," + cookie_valArr.length);
        for(var i=0;i<3;i++)
    {   
        alert(cookie_nameArr[i] + ", " + cookie_valArr[i]);

    }

});
}

</script></head>
</body></html>
Share Improve this question edited Jun 2, 2011 at 18:45 Bill Eisenhauer 6,1832 gold badges31 silver badges28 bronze badges asked Jun 2, 2011 at 12:10 user781022user781022 311 gold badge1 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

First you need to figure out what you are looking for. It's hard to look for a black cat in a dark room, especially if it is not there.

Open Chrome dev tools on the page you are interested in and check Resources tab. Do you see your cookie under "Cookies"? If yes, then you will be able to read it with your method. If not, then check your page url - maybe it is passed as url parameter? If not, then maybe session is used only for ajax requests? Maybe it is not used at all?

You need to find it manually first, then do it programmatically. Sessions e in all shapes and sizes, each site has its own way of implementing it. You can't build an extension that reads session for all sites.

Your approach is pretty much the way to go, just there is an easy way of inspecting objects using console instead of alert:

    chrome.cookies.getAll({domain: "stackoverflow."}, function(cookies){
        console.log(cookies);
    });

Click on background.html link on your extensions tab and check the console.

发布评论

评论列表(0)

  1. 暂无评论