Sessioning with XMLHttpRequest
by Dionysios G. Synodinos


Figure 2:


HTTP/1.1 200 OK 
Content-Length: 361 
Connection: close 
Content-Type: application/xml

<?xml version="1.0"?>
<CheckSessionParameters>
   <location>Library</location>
   <signal_strength>Good</signal_strength>
   <signal_quality>Medium</signal_quality>
   <bytes_in>754654</bytes_in>
   <bytes_out>211654</bytes_out>
</CheckSessionParameters>


Listing One

<html>
<head>
<title>Check session</title>
<script src="CheckSession.js" language="javascript" 
                                             type="text/javascript"></script>
   <style>
   .label {display:inline; font-weight:bold}
   .msg {display:inline; font-weight:normal}
</style>
</head>
<body onLoad="doLoad();">
<div class="label">Your session is: </div>
<div id="session" class="msg">initializing</div><br>
<div class="label">You are located at the: </div>
<div id="location" class="msg">initializing</div><br>
<div class="label">Signal strength: </div>
<div id="signal_strength" class="msg">initializing</div><br>
<div class="label">Signal quality:</div>
<div id="signal_quality" class="msg">initializing</div><br>
<div class="label">Bytes in: </div>
<div id="bytes_in" class="msg">initializing</div><br>
<div class="label">Bytes out: </div>
<div id="bytes_out" class="msg">initializing</div><br>
<div class="label">Refreshing in: </div>
<div id="refreshing" class="msg">
   <form name="counter"  class="msg">
      <input type="text" name="seconds" value="0" size="3">
   </form>
</div>
</body>


Listing Two

<?php
header('Content-Type: text/xml');

// Do the necessary calculation to produce $location, $signal_strength, 
// $signal_quality, $bytes_in and $bytes_out
?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<CheckSessionParameters>
    <location><?php echo $location></location>
    <signal_strength><?php echo $signal_strength></signal_strength>
    <signal_quality><?php echo $signal_quality></signal_quality>
    <bytes_in><?php echo $bytes_in></bytes_in>
    <bytes_out><?php echo $bytes_out></bytes_out>
</CheckSessionParameters>


Listing Three

(a)
var period = 300; 
var elapsed = 0; 

(b)
function doLoad() {
   CheckSession();
   setInterval( "CheckSession()", period*1000 );
   setInterval( "Counter()", 1000 );
}

(c)

function Counter() {
   if (period-elapsed) {
      elapsed = elapsed + 1 ;
      document.counter.seconds.value = (period-elapsed);
   } else {
      elapsed = 0;
   }
}


(d)

function CheckSession() {
    try {
        loadXMLDoc("SESSION_DATA.php");
    } catch(e) {
       alert("Error: " + e);
    }
}


Listing Four

(a)

function loadXMLDoc(url) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
   req.open("GET", url, true);
   req.send(null);
    } else if (window.ActiveXObject) {
        isIE = true;
            req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send();
     }
    }
}


(b)

function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
   if (req.status == 200) {
            printSuccess();
        } else {
            printFailure();
        }
  }
}


Listing Five

function printSuccess() {
    res = req.responseXML;
    document.getElementById("session").lastChild.nodeValue = 'Active';
    document.getElementById("location").lastChild.nodeValue =
        res.getElementsByTagName('location')[0].firstChild.nodeValue;
    document.getElementById("signal_strength").lastChild.nodeValue =
        res.getElementsByTagName('signal_strength')[0].firstChild.nodeValue;
    document.getElementById("signal_quality").lastChild.nodeValue =
        res.getElementsByTagName('signal_quality')[0].firstChild.nodeValue;
    document.getElementById("bytes_in").lastChild.nodeValue =
        res.getElementsByTagName('bytes_in')[0].firstChild.nodeValue;
    document.getElementById("bytes_out").lastChild.nodeValue =
        res.getElementsByTagName('bytes_out')[0].firstChild.nodeValue;
    elapsed = 0;
}
function printFailure() {
    res = req.responseXML;
document.getElementById("session").lastChild.nodeValue =   
'Inactive';
document.getElementById("location").lastChild.nodeValue = 
"Unknown";  
document.getElementById("signal_strength").lastChild.nodeValue = 
"No signal";
document.getElementById("signal_quality").lastChild.nodeValue = 
"No signal";
    elapsed = 0;
}



3


