Forum Moderators: coopster
I'm not sure how to do this so I don't know if I need to use PHP or Javascript.
What I have is a Webcam on a page and I want to know what users are viewing it right now. Of course all I would be able to see is their IP.
I want to know how many users are viewing the camera right now and how long they have been on the page.
The camera is setup to be a video feed on a PHP page.
Does anyone know how to do this?
thanks in advance for your help! :)
Wes
I was able to make something out of session variables. Instead of counting current users I have it set to record to the database the time they access the page and the time they leave the page. In order for it to record when they left they have to go to another page on the site. If not it doesnt record them leaving :P
still works good though.
Thanks again for your help :)
Sorry I said WebCam, just wanted to make it simple for people to get the idea of what I was doing.
I'm sure there's a better way to write this code, but this is a good starting point. It's the shell I use whenever I do XMLHttpRequest stuff.
The PHP pseudo code (active.php)
<?php
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
// log it to a database, flat file, whatever
$current_ip = $_SERVER['REMOTE_ADDR'];
// return a result or an error
$count_of_users = time();
// send the XML
if ($error == true) {
echo '<?xml version="1.0" standalone="yes"?><tell><result>error</result><message>User count temporarily unavailable.</message></tell>';
die;
} else {
// Success!
echo '<?xml version="1.0" standalone="yes"?><tell><result>success</result><message>There are currently '.$count_of_users.' users viewing this page.</message></tell>';
}
?>
The javascript code (active.html)
<script type="text/javascript">
// <![CDATA[
var req;
var url = 'active.php';
function loadXMLDoc() {
var connectstring = (url);
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", connectstring, true);
req.send(null);
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = processReqChange;
req.open("GET", connectstring, true);
req.send();
}
}
}
function processReqChange() {
// only if req shows "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
// Use the XML DOM to get the results
var xmlDocument = req.responseXML;
var result = xmlDocument.getElementsByTagName('result').item(0).firstChild.data;
var message = xmlDocument.getElementsByTagName('message').item(0).firstChild.data;
document.getElementById('result').innerHTML = message;
isWorking = false;
} else {
//maybe send an error here
}
}
}
var timer = setInterval("loadXMLDoc()",7200);
// ]]>
</script>
<div id="result"></div>
<a href="#" onclick="loadXMLDoc(); return false;">Update Users</a>