View Full Version : Putting the NUMBER of users online on forum on a non-vB page?
I just want to have something on a webpage which is not in my forum's directory, which will say: XX Users online in forum!
How do I grab that number?
Dean C
02-06-2003, 06:33 PM
Stick this code somewhere on the page maybe?
$activeusers = "";
$loggedinusers = "";
if ($displayloggedin) {
$datecut=time()-$cookietimeout;
$loggedins=$DB_site->query_first("SELECT COUNT(*) AS sessions FROM session WHERE userid=0 AND lastactivity>$datecut");
$numberguest=$loggedins['sessions'];
$numbervisible=0;
$numberregistered=0;
$loggedins=$DB_site->query("SELECT DISTINCT session.userid,username,invisible,usergroupid
FROM session
LEFT JOIN user ON (user.userid=session.userid)
WHERE session.userid>0 AND session.lastactivity>$datecut
ORDER BY invisible ASC, username ASC");
if ($loggedin=$DB_site->fetch_array($loggedins)) {
$numberregistered++;
if ($loggedin['invisible']==0 or $bbuserinfo['usergroupid']==6) {
$numbervisible++;
$userid = $loggedin['userid'];
if ($loggedin['invisible'] == 1) { // Invisible User but show to Admin
$invisibleuser = '*';
} else {
$invisibleuser = '';
}
if ($loggedin['usergroupid'] == 6 and $highlightadmin) {
$username = "<b><i>$loggedin[username]</i></b>";
} else if (($mod["$userid"] or $loggedin['usergroupid'] == 5) and $highlightadmin) {
$username = "<b>$loggedin[username]</b>";
} else {
$username = $loggedin['username'];
}
eval("\$activeusers = \"".gettemplate('forumhome_loggedinuser')."\";");
}
while ($loggedin=$DB_site->fetch_array($loggedins)) {
$numberregistered++;
$invisibleuser = '';
if ($loggedin['invisible']==1 and $bbuserinfo['usergroupid']!=6) {
continue;
}
$numbervisible++;
$userid=$loggedin['userid'];
if ($loggedin['invisible'] == 1) { // Invisible User but show to Admin
$invisibleuser = '*';
}
if ($loggedin['usergroupid'] == 6 and $highlightadmin) {
$username = "<b><i>$loggedin[username]</i></b>";
} else if (($mod["$userid"] or $loggedin['usergroupid'] == 5) and $highlightadmin) {
$username = "<b>$loggedin[username]</b>";
} else {
$username = $loggedin['username'];
}
eval("\$activeusers .= \", ".gettemplate('forumhome_loggedinuser')."\";");
}
}
$DB_site->free_result($loggedins);
$totalonline=$numberregistered+$numberguest;
$numberinvisible=$numberregistered-$numbervisible;
$maxusers=explode(" ", gettemplate('maxloggedin',0,0));
if ((int)$maxusers[0] <= $totalonline) {
$time = time();
$maxloggedin = "$totalonline " . $time;
$DB_site->query("UPDATE template SET template='$maxloggedin' WHERE title='maxloggedin'");
$maxusers[0] = $totalonline;
$maxusers[1] = $time;
}
$recordusers = $maxusers[0];
$recorddate = vbdate($dateformat,$maxusers[1]);
$recordtime = vbdate($timeformat,$maxusers[1]);
eval("\$loggedinusers = \"".gettemplate('forumhome_loggedinusers')."\";");
Take from index.php
Regards
- miSt
You need global.php too though...
Would using phpinclude to include global.php then use that code work?
Dean C
02-09-2003, 10:04 AM
Of course you have to include global.php :)
But do it in the file and not the phpinclude template
- miSt
obviously not the php include template :p
You could:
1) include() config.php
2) connect to the database
3) SELECT count(*) as count FROM session
Originally posted by MUG
You could:
1) include() config.php
2) connect to the database
3) SELECT count(*) as count FROM session
I don't know much PHP/sql...could you tell me how to do this?
<?php
require('path/to/config.php');
$db = mysql_connect($servername,$dbusername,$dbpassword) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
unset($servername,$dbusername,$dbpassword,$dbname) ;
$query = mysql_query("SELECT count(*) as count FROM session WHERE lastactivity > ". (time()-3600) ) or die(mysql_error()); //one hour
list($num_online) = mysql_fetch_array($query);
mysql_free_result($query);
mysql_close($db);
echo $num_online;
?>
Is the 3600 in there the timeout? Say if I have timeout set at 900 seconds on my forums, would I change it to 900 in that code?
If that is so, then it's showing the incorrect number. 21 on my forum (guests + members) but on my other page I put that code on it says 26. I'm not sure why. There's 3 invisible users online.
What could be the cause of this?
Time zone differences. The code I posted is based on GMT time.
How can I make it so both pages show the same figure?
ie. Guests will see the default I have set from the adminCP.
Members will see what they have set in their userCP.
Is it possible to synchronize this into the page?
Or is there something simpler? Or is it servertimezone?
Strange... look at the code from index.php that Mist posted. It's using the $cookietimeout variable which is loaded from the Admin CP. It's doing nearly the same thing as what I did for guests, but the code to generate the who's online member list and the # of registered members online is very confusing.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.