View Full Version : I *really* need help with getting this online users thing working..
Okay.. I want to be able to show on an external page (outside of vbulletin) the number of total regged users, and the number of online users..
This code was workign PERFECTLY with vB114 but it produces an error with vb2..
heres the code..
<?
require("forum/admin/config.php");
$db=mysql_connect($servername,$dbusername,$dbpassw ord);
mysql_select_db($dbname);
$loggedins=mysql_query("SELECT COUNT(sessionid) AS sessions FROM session",$db);
$tempa=mysql_fetch_array($loggedins);
$totalonline=$tempa[sessions];
$registered=mysql_query("SELECT COUNT(userid) AS registered FROM user",$db);
$tempb=mysql_fetch_array($registered);
$regged=$tempb[registered];
echo "<font face=arial size=2>
We have a total of $regged registered users. There are currently $totalonline users online at the moment.</font>
";
?>
and now heres what it shows with vb 20 installed..
"Warning: 0 is not a MySQL result index in /home/redg/htdocs/online2.php on line 7
We have a total of 3130 registered users. There are currently users online at the moment. "
any ideas are greatly appreciated..
slip
<?
require("forum/admin/config.php");
$db=mysql_connect($servername,$dbusername,$dbpassw ord);
mysql_select_db($dbname);
$loggedins=mysql_query("SELECT COUNT(userid) AS users FROM session",$db) or die ("oops");
while($loggedin = mysql_fetch_array($loggedins))
$totalonline=number_format($loggedin[users]);
$users=mysql_query("SELECT COUNT(userid) AS registeredusers FROM user",$db) or die ("double oops");
while($user = mysql_fetch_array($users))
$totalusers=number_format($user[registeredusers]);
echo "<font face=arial size=2>
We have a total of $totalusers registered users. There are currently $totalonline users online at the moment.</font>
";
?> should do it...
[edited to correct invalid query]
i did that, and i only get "oops" now when i go to that file.. any ideas?
Yeah... I just checked the session table and there is no such field as 'sessionid'.
Replace your first query with this: "SELECT COUNT(userid) AS users FROM session"
woo hoo!
you seriously rule!!! :D :D :D
thanks!
is there a way to put this into a html page?
Originally posted by Blue2000
is there a way to put this into a html page? Do you mean an HTML page as in a page with a .html extension? If so, then no, unless your server has been configured to parse HTML files as PHP.
ahh, but there is ;)
SSI from the .php file
Originally posted by Flare945
ahh, but there is ;) Oh, okay then ;)
The results in VBulletin 2.0 are different to the results in your script. It counts correctly the sessions, but why shows the vbulletin other results? Any suggestions?
yeah, this script "works" but it doesnt really work.
for example when i have 60 users online it says there are 120, and it keeps going up, and then every now and then it will drop down to the actual number..
any ideas..?
I think the online vbulletin script works with cookies and with a timeout option. The onlinescript by Slip counts all userid's in "session". So the results are different.
Compare in index.php from vbulletin:
if ($displayloggedin) {
$datecut=time()-$cookietimeout;
//$loggedins=$DB_site->query_first("SELECT COUNT(*) AS sessions FROM session WHERE lastactivity>$datecut");
//$totalonline=$loggedins['sessions'];
$loggedins=$DB_site->query_first("SELECT COUNT(*) AS sessions FROM session WHERE userid=0 AND lastactivity>$datecut");
$numberguest=$loggedins['sessions'];
//$numberregistered=$totalonline-$numberguest;
would anyone mind telling me how to implement that?
sorry, i'm incredibly stupid when it comes to PHP stuff..
Well, you could require() the global.php file and then you could use the $cookietimeout variable. Or you could pull it from the database yourself, either way (it's in the settings table, I believe). :) But then, you would adjust the code:
<?
require("forum/admin/config.php");
$datecut = time()-$cookietimeout;
$db=mysql_connect($servername,$dbusername,$dbpassw ord);
mysql_select_db($dbname);
$loggedins=mysql_query("SELECT COUNT(userid) AS users FROM session WHERE lastactivity>$datecut AND userid=0",$db) or die ("oops");
while($loggedin = mysql_fetch_array($loggedins))
$totalonline=number_format($loggedin[users]);
// rest deleted to save space, but leave it in there!
?>
I added the part in red
That's what I thought, and it got rid of the cumulative effect of the previous code.
But now it insists there are 0 people online :(
Is it possible to add the number of "guests" currently online as well?
Thanks :)
-Hideki
anyone want to help with fixing this?
I have been looking at it...give me some time..
It would be coool if it actually showed the members (who)that are online like index.php
OK I think I goofed. I looked at the sessions table, so here goes.
For registered members, you need it where userid>0. For guests, it's where userid=0. So let's revisit the code:
<?
require("forum/admin/config.php");
$db=mysql_connect($servername,$dbusername,$dbpassw ord);
mysql_select_db($dbname);
$cookietimeout = mysql_query("SELECT value FROM setting WHERE varname = cookietimeout");
$datecut = time()-$cookietimeout;
$loggedins=mysql_query("SELECT COUNT(userid) AS users FROM session WHERE lastactivity>$datecut AND userid>0",$db) or die ("oops");
while($loggedin = mysql_fetch_array($loggedins))
$membersonline=number_format($loggedin[users]);
$guestsloggedins=mysql_query("SELECT COUNT(userid) AS users FROM session WHERE lastactivity>$datecut AND userid=0",$db) or die ("oops");
while($guestsloggedin = mysql_fetch_array($guestsloggedins))
$guestsonline=number_format($guestsloggedin[users]);
// rest of code to display numbers
?>
New code is in red. (I also changed a couple variables, but the new code is highlighted to point out what we did wrong.)
I also added a couple lines to set the date cut without involving global.php.
Use $membersonline to display the members online, and use $guestsonline to display the guests.
Thanks For Your Help
But can you post the whole code?
I didnt understand your directions!
Thanks
Sorry, I changed the code but it shows me 0members and 0 visitors!
;)
You are on the right way, but there should be one mistake...
Couldnt get it to work either
Hmm. I noticed the 0 people yesterday, with different settings.
Just tried out:
I just changed "$datecut = time()-$cookietimeout;" into "$datecut = time()-300;"
And that worked. It just means it shows who's online in the last 5 minutes...
Can it be that the $cookietimeout query doesn't return a number but plain text?
This works. I have the timeout settings on 500 seconds in the vbulletin board, so the working skript looks now like that: (remember the right path to the admin/config file!)
<?
require("admin/config.php");
$db=mysql_connect($servername,$dbusername,$dbpassw ord);
mysql_select_db($dbname);
$datecut = time()-500;
$loggedins=mysql_query("SELECT COUNT(userid) AS users FROM session WHERE lastactivity>$datecut AND userid>0",$db) or die ("oops");
while($loggedin = mysql_fetch_array($loggedins))
$membersonline=number_format($loggedin[users]);
$guestsloggedins=mysql_query("SELECT COUNT(userid) AS users FROM session WHERE lastactivity>$datecut AND userid=0",$db) or die ("oops");
while($guestsloggedin = mysql_fetch_array($guestsloggedins))
$guestsonline=number_format($guestsloggedin[users]);
$users=mysql_query("SELECT COUNT(userid) AS registeredusers FROM user",$db) or die ("double oops");
while($user = mysql_fetch_array($users))
$totalusers=number_format($user[registeredusers]);
echo "<font face=arial size=2>
We have a total of $totalusers registered users. There are currently $membersonline members and $guestsonline guests online.</font>
";
?>
Hey Peter, I tried your exact code,
and it listed 3 members online,
But the thing is.... it'sa test board and I only have 2 members total!
I don't think it works.
Can anyone make this actually list the members name like in the index.php file?
hmm, thats right, something is more wrong, i have nearly the right results like in the vbulletib forum, but only nearly, so i thought at the beginning, the code is right. Sorry, I was to fast ;)
Originally posted by Pingu
I just changed "$datecut = time()-$cookietimeout;" into "$datecut = time()-300;"
And that worked. It just means it shows who's online in the last 5 minutes...
Can it be that the $cookietimeout query doesn't return a number but plain text?
I made a mistake in the code and therefore it was getting a MySQL error back instead of the cookietimeout. The corrected code is below.
Originally posted by Sarge
...and it listed 3 members online, but the thing is.... it'sa test board and I only have 2 members total!
I think I figured this out too - adding "DISTINCT" should fix it. See the corrected code below.
OK after a bit of scratching my head, I think I have this thing figured out.
<?
require("forum/admin/config.php");
$db=mysql_connect($servername,$dbusername,$dbpassw ord);
mysql_select_db($dbname);
$cookietimeout = mysql_query("SELECT value FROM setting WHERE varname = 'cookietimeout'");
$datecut = time()-$cookietimeout[value];
$loggedins=mysql_query("SELECT DISTINCT COUNT(userid) AS users FROM session WHERE lastactivity>$datecut AND userid>0",$db) or die ("oops");
while($loggedin = mysql_fetch_array($loggedins))
$membersonline=number_format($loggedin[users]);
$guestsloggedins=mysql_query("SELECT COUNT(userid) AS users FROM session WHERE lastactivity>$datecut AND userid=0",$db) or die ("oops");
while($guestsloggedin = mysql_fetch_array($guestsloggedins))
$guestsonline=number_format($guestsloggedin[users]);
$users=mysql_query("SELECT COUNT(userid) AS registeredusers FROM user",$db) or die ("double oops");
while($user = mysql_fetch_array($users))
$totalusers=number_format($user[registeredusers]);
echo "<font face=arial size=2>We have a total of $totalusers registered users. There are currently $membersonline members and $guestsonline guests online.</font>";
?>
Just remember to change the path to config.php, based on where you put this file. I think this should work now. *crosses fingers*
Now it lists 0 members and 0 guests online
OK I figured it out, finally. Change the code as follows. Replace this:
$cookietimeout = mysql_query("SELECT value FROM setting WHERE varname = 'cookietimeout'");
$datecut = time()-$cookietimeout[value];
with this:
$cookievalue = mysql_query("SELECT value FROM setting WHERE varname = 'cookietimeout'");
$cookietimeout = mysql_result($cookievalue, 0, 0);
$datecut = time()-$cookietimeout;
I've also attached the complete code in a file. Unzip the file online.php from it, and then open that file in Notepad (Windows) or Simpletext (Mac) and edit the path to config.php, if necessary. Upload it to where you want and then include it using PHP <? include("/path/to/online.php"); ?> or SSI <!--#include virtual="/path/to/online.php"-->.
Problems? Let me know. It should work now. :)
Works Like A Charm!
Thanks!
I'll work on getting to show the user names. Give me a few hours, and I'll release it in a new thread. :)
You rock, it worked great!
I couldn't get all of the code from the attachment though.
Jyaki: What do you mean? I just tried downloading it and it worked fine...?
When I click on it, This is everything I get:
$datecut AND userid>0",$db) or die ("oops"); while($loggedin = mysql_fetch_array($loggedins)) $membersonline=number_format($loggedin[users]); $guestsloggedins=mysql_query("SELECT COUNT(userid) AS users FROM session WHERE lastactivity>$datecut AND userid=0",$db) or die ("oops"); while($guestsloggedin = mysql_fetch_array($guestsloggedins)) $guestsonline=number_format($guestsloggedin[users]); $users=mysql_query("SELECT COUNT(userid) AS registeredusers FROM user",$db) or die ("double oops"); while($user = mysql_fetch_array($users)) $totalusers=number_format($user[registeredusers]); echo "We have a total of $totalusers registered users. There are currently $membersonline members and $guestsonline guests online."; ?>
I've changed it to a zip file. Let me know if you're still having problems.
I just copied your earlier code and made the changes you specified manually.
I've been wanting this one for a long time.
Thanks
As promised, I released the online users with user names. There's a link in my sig. Enjoy! :D
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.