Read Session Id using Javascript Read Session Id using Javascript javascript javascript

Read Session Id using Javascript


Yes. As the session ID is either transported over the URL (document.location.href) or via a cookie (document.cookie), you could check both for the presence of a session ID.


The following can be used to retrieve JSESSIONID:

function getJSessionId(){    var jsId = document.cookie.match(/JSESSIONID=[^;]+/);    if(jsId != null) {        if (jsId instanceof Array)            jsId = jsId[0].substring(11);        else            jsId = jsId.substring(11);    }    return jsId;}


you can receive the session id by issuing the following regular expression on document.cookie:

alert(document.cookie.match(/PHPSESSID=[^;]+/));

in my example the cookie name to store session id is PHPSESSID (php server), just replace the PHPSESSID with the cookie name that holds the session id. (configurable by the web server)