HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Polling script to determine the continuous time a user has been connected

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scriptthepollingusertimebeenhasconnecteddeterminecontinuous

Problem

I've been asked to keep track of how long a user has been connected to a site without interruption.

So far the solution I've come up with is to use ajax to poll the site every now and then to check how much time has elapsed since the last call.

Any suggestions for possible improvements would be appreciated.

P.S: Not sure why but text isn't displaying if no text is in the div or echoed.

The javascript


var page = "UserConnected.php";
var timer = ""; // Not sure how to avoid conflicts between timers

function toto(url,target)
 {
 //alert("working");
   document.getElementById(target).innerHTML = 'sending...';
   if (window.XMLHttpRequest) {
       req = new XMLHttpRequest();
       req.onreadystatechange = function() {ajaxDone(target);};
       //req.open("GET", url, true);
       //req.send(null);
           req.open("POST", page, true);
           req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
           req.send("data=id; ?>");
   // IE/Windows ActiveX version
   } else if (window.ActiveXObject) {
       req = new ActiveXObject("Microsoft.XMLHTTP");
       if (req) {
           req.onreadystatechange = function() {ajaxDone(target);};

           //req.open("GET", url, true);
           //req.send();
           req.open("POST", page, true);
           req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
           req.send("data=id; ?>");

       }
   }
           timer = setTimeout("toto(page,'scriptoutput')", 1 * 60  * 1000);
}

function ajaxDone(target) {
    // only if req is "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200 || req.status == 304) {
            results = req.responseText;
            document.getElementById(target).innerHTML = results;
        } else {
            document.getElementById(target).innerHTML="ajax error:\n" +
            req.statusText;
        }
    }
}


The PHP code

```
if( isset( $_POST["data"] ) ){
$id = (int) $_POST

Solution

Structure of your Javascript is tightly coupled with your logic, decoupling will allow change without major changes.

a) Move you Ajax initialization [xmlHttp] to a usable function.

b) UnIntended display of UserId req.send("data=id; ?>"); as plain text inside your javaScript is security risk

c) Refactor your client script and least packit(dean edwards works ok) and compress it.

d) Using a comet based server is Best solution to this rather than polling the server at defined intervals. I would recommend you learn APE Project

another point! you can do it this way, when user navigates away from page ask them a few seconds to send ajax request. You have login time and now you have leaving time , calculate Hit rate yourself.
Also using LocalStorage will be viable option too

Context

StackExchange Code Review Q#2795, answer score: 2

Revisions (0)

No revisions yet.