www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Getting just the session ID number

  1. #1
    Join Date
    Nov 2010
    Location
    Colorado
    Posts
    8

    Getting just the session ID number

    Hi Folks,

    This is my first post, so I hope it's in the right place and thank any and all for advice and help.

    I'm working on a big JS project and need to pull the session ID out (JUST the ID number, not the whole thing that comes out of DOM). For example:

    Code:
    *********************************************
    SESSION ID :-> 0123ABC345DEF678
    HOST NAME :-> foo.com
    SERVER NAME :-> <<serrvername>>
    JVM NAME :-> <<name>>
    TIME :-> Thu Oct 28 01:36:11 CDT 2010 *********************************************
    I need a help pulling out JUST the session ID number: 0123ABC345DEF678 for this example and just throw it into an onClick event or something to help me test down the line. For background -- this is all being dumped in an unprotected HTML file! This is part of a huge task that would take an eternity to explain, but this small part I can't figure out.

    Thanks All,

    -NL80

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    By short, the very first thing you must do is to write that server-side variable, SESSION ID, as a JavaScript variable, most likely a Global one. The syntax depends on the server-side language you are using. For instance, in PHP you may echo something like (untested):
    PHP Code:
    <?php
    $ses_id 
    session_id(); 
    echo 
    "<script type=\"text/javascript\">var sessionID='".$ses_id."';</script>"
    ?>

  3. #3
    Join Date
    Nov 2010
    Location
    Colorado
    Posts
    8

    no access to server side

    Thanks for the reply! Unfortunately this is all client side and has to be JS (requirement for the project). I come from the perl world and it's killing me to do it this way. Any other ideas?

  4. #4
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    that looks like return data from a Java app not JS deosnt it?

  5. #5
    Join Date
    Nov 2010
    Location
    Colorado
    Posts
    8

    what I was given

    I was given this as an example and have an analytics framework that is in ALL javascript and tried to tell my boss there are better solutions but it would destroy the dozens of JS files...so I was given this (I stripped out the real names of course), told it is being dumped into an HTML file. I need to use JS to access and parse that session ID. Kor had a very similar solution to what I proposed (in perl); but that's what I'm up against and STUMPED. Any help would be appreciated. Thanks!

    NL80

  6. #6
    Join Date
    Apr 2010
    Location
    Salem,Ma
    Posts
    623
    I'm still confused here. Where is the original sessionid coming from exactly? Im not an expert but from what I understand session id's are typically only created with server side code of some kind, then you use somethng liek kor's example to turn it into a js variable. Also from what Ive seen if your using .asp pages ( my work site is mainly vbscript and classic asp on IIS) they automatically save a sessionid in a cookie for that page.

    you can use a custom method in js to create your own based on random or other similar methods, like this: http://usefulscripts.wordpress.com/2...or-id-cookies/
    Last edited by DanInMA; 11-02-2010 at 11:28 PM.

  7. #7
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Sir, let's see: You need to grab a server-side session variable but you are suppose to use only JavaScript for that? How comes that? It looks like you have no server-side access to the server DataBase, which makes your problem impossible to solve. JavaScript is a client-side language, not a server-side one. The only way you can pass a server-side variable to a client-side language is to use a server-side language as a data vector.

  8. #8
    Join Date
    Nov 2010
    Location
    Colorado
    Posts
    8
    Thanks guys,

    let me re-phrase. The code block below is inserted into a COMMENT within the HTML page. So I need a way to read the comments out of the HTML and get my ID after that. I was unaware of the comment issue:
    Code:
    <!--
    SESSION ID :-> 0123ABC345DEF678
    HOST NAME :-> foo.com
    SERVER NAME :-> <<serrvername>>
    JVM NAME :-> <<name>>
    TIME :-> Thu Oct 28 01:36:11 CDT 2010 
    -->
    Thanks!

  9. #9
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title></title>
    
    <script type="text/javascript">
    function getFile(url) {
      if (window.XMLHttpRequest) {              
        AJAX=new XMLHttpRequest();              
      } else {                                  
        AJAX=new ActiveXObject("Microsoft.XMLHTTP");
      }
      if (AJAX) {
         AJAX.open("GET", url, false);                             
         AJAX.send(null);
         return AJAX.responseText;                                         
      } else {
         return false;
      }                                             
    }
    
    var fileContents = getFile('session.htm'); // the file
    var needle = /SESSION ID :-> (\w+)/ig;
    fileContents.match(needle);
    alert("Session ID: "+RegExp.$1);
    </script>
    
    </head>
    <body>
    
    </body>
    </html>

  10. #10
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by nittanyLion80 View Post
    Thanks guys,

    let me re-phrase. The code block below is inserted into a COMMENT within the HTML page. So I need a way to read the comments out of the HTML and get my ID after that. I was unaware of the comment issue:
    Code:
    <!--
    SESSION ID :-> 0123ABC345DEF678
    HOST NAME :-> foo.com
    SERVER NAME :-> <<serrvername>>
    JVM NAME :-> <<name>>
    TIME :-> Thu Oct 28 01:36:11 CDT 2010 
    -->
    Thanks!
    Well, here you are! That changes a lot. Is this comment on the same page where you want to place the javascript code? If not, and it is about two different pages, do they belong to the same domain?

  11. #11
    Join Date
    Nov 2010
    Location
    Colorado
    Posts
    8
    Thanks Fang!

    The solution works perfectly, but not certain my client will want it implemented that way. If you have an alternate generic JS solution I would be all for it. Thanks for your help!

  12. #12
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    You could load the session file into a hidden iframe and read it from there

  13. #13
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    You may "read" the comment only on first onload and store it for later usage in a JavaScript cookie. Or in Local Storage, if HTML5 compliant browsers are the target.

  14. #14
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    To grab that value, could be something like:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>untitled</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <script type="text/javascript">
    function findSessionId(){
    var childs=document.getElementsByTagName('body')[0].childNodes, c, id, i=0;
    while(c=childs[i++]){
    	if(c.nodeType==8&&c.nodeValue.match(/SESSION ID/)){
    		id=c.nodeValue.split('HOST NAME')[0].split('->')[1];
    		alert(id);break;
    
    	}
    }
    }
    onload=findSessionId
    </script>
    </head>
    <body>
    <!--
    SESSION ID :-> 0123ABC345DEF678
    HOST NAME :-> foo.com
    SERVER NAME :-> <<serrvername>>
    JVM NAME :-> <<name>>
    TIME :-> Thu Oct 28 01:36:11 CDT 2010 
    -->
    <div></div>
    <!--
    other comment
    -->
    </body>
    </html>
    rough code, but it can be refined to get rid of the empty spaces on using a RegExp trimming
    Last edited by Kor; 11-03-2010 at 04:11 PM.

  15. #15
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Every legal written node in DOM has a nodeType property with a certain value. A comment has the value=8
    https://developer.mozilla.org/en/nodeType
    The nodeValue property returns a string of the value of that node, if any. Note that some nodes have no values, for instance an element node (a tag)
    Last edited by Kor; 11-03-2010 at 04:30 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles