Forum Moderators: coopster

Message Too Old, No Replies

How to show the number of users on one page.

PHP or Javascript?

         

wfernley

4:57 pm on Nov 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey everyone,

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

lobo235

4:03 am on Nov 3, 2005 (gmt 0)

10+ Year Member



You can do this with sessions. I would create a MySQL table to store the session ids of each visitor and the time of their last page request. Then just count the number of users that requested the page within the last 5-10 mins or something like that. Anyone else have any other ideas?

wfernley

5:49 pm on Nov 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks lobo235.

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 :)

ergophobe

5:58 pm on Nov 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't know how the web cam refresh works, but don't you know every time there's a refresh that the visitor is still there?

So if it refreshes every 5 mins, anyone who has a session time older than 5 minutes, isn't there anymore. If it refreshes once a day, that's another issue.

wfernley

6:13 pm on Nov 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It actually is not a WebCam its a Network Camera. Similar to a WebCam but it has a WebServer built in. It uses ActiveX to display the video. There is no refreshing. So someone could stay logged into the camera for as long as they want the page doesnt have to refresh at all. ;) It's pretty cool stuff.

Sorry I said WebCam, just wanted to make it simple for people to get the idea of what I was doing.

ergophobe

6:27 pm on Nov 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I guess in that case you would need a way of "pinging" every so often.

whoisgregg

11:34 pm on Nov 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could use javascript to ping a PHP page every x seconds. That page can log the requesting IP, and also return an updated value for the current number of users.

wfernley

2:05 pm on Nov 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah thats an idea too. Only thing is I don't know JavaScript. I don't mind what I have setup now. I know PHP so I know how it works :) makes it easier...

whoisgregg

5:04 pm on Nov 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, I would use the XMLHttpRequest Object to do this with a PHP script that does logging and returns the number of users in XML data. It's easier than it sounds. You can drop these two files in the same directory and it will work right away. Of course, you'll need to write the actual logging of active users code, unless a unix timestamp is close enough to your actual user numbers. :D

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>