Read Session Id using Javascript

前端 未结 7 1866
被撕碎了的回忆
被撕碎了的回忆 2020-12-24 06:33

Is it by any means possible to read the browser session id using javascript?

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-24 06:50

    Here's a short and sweet JavaScript function to fetch the session ID:

    function session_id() {
        return /SESS\w*ID=([^;]+)/i.test(document.cookie) ? RegExp.$1 : false;
    }
    

    Or if you prefer a variable, here's a simple one-liner:

    var session_id = /SESS\w*ID=([^;]+)/i.test(document.cookie) ? RegExp.$1 : false;
    

    Should match the session ID cookie for PHP, JSP, .NET, and I suppose various other server-side processors as well.

提交回复
热议问题