View Full Version : Counting users online
vbdarwin
11-05-2010, 03:20 PM
Hi all!
I already posted this request into the wrong section, and a Mod suggested me to post here, so sorry for double post.
I'm searching an easy way to count (every n minutes) users online (both members and guests), display the value and write the value to a db table.
I need this for statistics, monitoring and optimization purposes.
Unfortunately, I'm not a php programmer... :o
Any ideas?
Thank you
Here's something simple that might do it:
1) Put this in a file named something like usercount.php (or whatever you want) and put it in includes/cron.
<?php
// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);
if (!is_object($vbulletin->db))
{
exit;
}
// SQL for creating table, do once before starting task (add your table prefix to the table name
//CREATE TABLE usercounts (
//time INT UNSIGNED NOT NULL DEFAULT '0',
//users SMALLINT UNSIGNED NOT NULL DEFAULT '0',
//guests SMALLINT UNSIGNED NOT NULL DEFAULT '0')
$datecut = TIMENOW - $vbulletin->options['cookietimeout'];
$guests = $vbulletin->db->query_first("
SELECT COUNT(*) AS guests
FROM " . TABLE_PREFIX . "session
WHERE lastactivity > $datecut AND userid = 0
");
$users = $vbulletin->db->query_first("
SELECT COUNT(DISTINCT userid) AS users
FROM " . TABLE_PREFIX . "session
WHERE lastactivity > $datecut AND userid != 0
");
$vbulletin->db->query_write(
"UPDATE " . TABLE_PREFIX . "usercounts
VALUES(" . TIMENOW . ", $users[users], $guests[guests])
");
log_cron_action('', $nextitem, 1);
?>
2) Create a table in your database called usercounts (with your table prefix in front of it if you have one). I put the SQL for creating the table in a comment in the code above.
3) Add a scheduled task using ./includes/cron/usercount.php as the file name. My guess is that it won't make any sense to run the task at a shorter interval than your session timeout is set to.
A couple other things - scheduled tasks don't run if no one visits your board, so during times of no users (if you have any) you won't have counts. Also, if your board is very busy I think there's a chance that the task will occasionally run twice. But since the records have a timestamp, you should be able to deal with either of those things when analyzing your data.
I tested this a little, but not that much so you'll probably want to test it some before counting on it to work right.
vbdarwin
11-06-2010, 06:52 AM
Hello kh99
thank you very much for your help!
Unfortunately, the script not worked for me. The table remains empty...
Furthermore... is there a way to have the three valued visualized on the screen? Or maybe this impact on the cron?
Thank you
--------------- Added 1289032837 at 1289032837 ---------------
Additional info: the script exit here:
if (!is_object($vbulletin->db))
{
echo "Exit";
exit;
}
Furthermore... is there a way to have the three valued visualized on the screen? Or maybe this impact on the cron?
Thank you
Sorry, I forgot about the display part. Change the end of the usercount.php file to look like this (add the one build_data_store line above the log_cron_action):
build_datastore('usercount', serialize(array('users' => $users['users'], 'guests' => $guests['guests'])), 1);
log_cron_action('', $nextitem, 1);
?>
Next you need to create a plugin using hook location init_start with this code:
$datastore_fetch[]="'usercount'";
Then in a template where you want to display it add something like:
<if condition="!empty($vbulletin->usercount)">
{$vbulletin->usercount[users]} Users, {$vbulletin->usercount[guests]} Guests
</if>
The 'if' is there in case it's not in the datastore yet. If that's a problem you could probably do something more complicated with a plugin to calculate the values if they aren't in the datastore, but probably it won't be necessary.
Unfortunately, the script not worked for me. The table remains empty...
I have no idea why the script would exit there. That's "standard" code that I copied from another cron file, and it appears in a lot of them. I tried it myself and had no problem. You're adding it using the "scheduled task manager", right? Maybe you can compare what you have to another php file in includes/cron and see if there's some extra character or space somewhere.
Or maybe someone else has an idea?
ETA: I could make it all a product and post an xml file to import if you'd like.
Lynne
11-06-2010, 02:01 PM
You need to run that script as a cron job/scheduled task, not as a standalone file.
vbdarwin
11-06-2010, 02:29 PM
You need to run that script as a cron job/scheduled task, not as a standalone file.
My problem is here... I have no idea about how use the scipt as a cron task.
I'm very sorry for my inexperience.
It is possible to turn it as a standalone php script?
Thank you.
I suppose there's some way to make it a stand-alone script and use an actual cron job. But it's easy to create a scheduled task. In the admin control panel, under "Scheduled Tasks" click on "Add New Scheduled Task" and fill in the fields. There's help available for each field by clicking on the ? icons on the right, but most of the text ones don't really matter, you just have to make something up. If you need more help you could go to the vbulletin manual (http://www.vbulletin.com/docs/html/) and read about it.
To make it run periodically, select the times of the hour you want it to run from the drop-down menus next to "Minutes". For instance if you want it to run every 15 minutes you might choose 15, 30, 45, and 00. Leave all the other time fields as '*' or '-'.
When you get to the 'Filename' field, enter the name of the file (you just have to fill in 'usercount' before the .php).
Save it and that's it. You can wait for it to run or you can go to "Scheduled Task Manager" and press the "Run Now" button next to that task to force it to run whenever you want.
[deleted this, see below instead]
Lynne
11-06-2010, 08:08 PM
What you forgot to say is you still need that file and it should be uploaded to the /includes/cron directory (just in case he thinks all he needs is the product file).
Yes, I see now that what he is probably looking for is a complete mod he can simply install and have it all working. I'm afraid even if this works it won't have all the needed features.
So, if anyone else wants to take that on please do. vbdarwin, if you want to start another thread and maybe refine your request I'll let someone else answer that one.
vbdarwin
11-07-2010, 02:16 PM
Here's an xml file you can import as a product if you want, it will create the scheduled task and plugin for you. You still need to make the changes to includes/cron/usercount.php yourself.
ETA: I don't know what else I can do, if someone else thinks they can improve on this or has a different solution feel free to jump in.
Hello all, and thank you again for the support.
The resulting code should be like this:
<?php
// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);
if (!is_object($vbulletin->db))
{
exit;
}
// SQL for creating table, do once before starting task (add your table prefix to the table name
//CREATE TABLE usercounts (
//time INT UNSIGNED NOT NULL DEFAULT '0',
//users SMALLINT UNSIGNED NOT NULL DEFAULT '0',
//guests SMALLINT UNSIGNED NOT NULL DEFAULT '0')
$datecut = TIMENOW - $vbulletin->options['cookietimeout'];
$guests = $vbulletin->db->query_first("
SELECT COUNT(*) AS guests
FROM " . TABLE_PREFIX . "session
WHERE lastactivity > $datecut AND userid = 0
");
$users = $vbulletin->db->query_first("
SELECT COUNT(DISTINCT userid) AS users
FROM " . TABLE_PREFIX . "session
WHERE lastactivity > $datecut AND userid != 0
");
$vbulletin->db->query_write(
"UPDATE " . TABLE_PREFIX . "usercounts
VALUES(" . TIMENOW . ", $users[users], $guests[guests])
");
build_datastore('usercount', serialize(array('users' => $users['users'], 'guests' => $guests['guests'])), 1);
$datastore_fetch[]="'usercount'";
log_cron_action('', $nextitem, 1);
?>
Is it correct?
I apologize for making it confusing, so let's start over from the beginning.
1) Drop the table you created from the database (usercounts), delete includes/cron/usercount.php, and if you created a scheduled task or plugin, delete them.
2) Save the usercount.php attachment from this post and ftp it to includes/cron
3) Save the product-usercount.xml attachment and import it as a product. This will create the database table, the plugin, and the scheduled task.
4) To display the values put something like this code in a template:
<if condition="!empty($vbulletin->usercounts)">
{$vbulletin->usercounts[members]} Members, {$vbulletin->usercounts[guests]} Guests
</if>
Notes:
- The scheduled task is set to run every 15 minutes. If you want to run it at a different interval, you can use the Scheduled Task Manager to change it.
- I changed the database column "users" to "members" so it's not quite the same as the previous code.
- The counts will not display until the task runs at least once, so you can go to the "Scheduled Task Manager" and press the "Run Now" button to force it to run once if you want to.
- If you uninstall the product, it won't remove the database table.
Lynne
11-07-2010, 04:27 PM
If they had Reputation here, I know I'd be repping kh99 for all his help! :)
vbdarwin
11-08-2010, 05:23 AM
kh99, I have no words to thank you for the wonderful job! :up:
Only, I'm not able to format 'time' to a readable value. :confused:
However, this WORK and THANK YOU ;)
--------------- Added 1289205457 at 1289205457 ---------------
Found!
FROM_UNIXTIME(time)
Found!
FROM_UNIXTIME(time)
I'm glad that worked out - we could change the column type for the time if you wanted to.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.