Go Back   vb.org Archive > Community Discussions > Modification Requests/Questions (Unpaid)
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 11-05-2010, 03:20 PM
vbdarwin vbdarwin is offline
 
Join Date: Nov 2010
Posts: 11
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Counting users online

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...

Any ideas?

Thank you
Reply With Quote
  #2  
Old 11-05-2010, 10:01 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 Code:
<?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(''$nextitem1);

?>
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.
Reply With Quote
  #3  
Old 11-06-2010, 06:52 AM
vbdarwin vbdarwin is offline
 
Join Date: Nov 2010
Posts: 11
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 [DATE]1289032837[/DATE] at [TIME]1289032837[/TIME] ---------------

Additional info: the script exit here:
Code:
if (!is_object($vbulletin->db))
{
    echo "Exit";
    exit;
}
Reply With Quote
  #4  
Old 11-06-2010, 01:58 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
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):
PHP Code:
build_datastore('usercount'serialize(array('users' => $users['users'], 'guests' => $guests['guests'])), 1);

log_cron_action(''$nextitem1);

?> 

Next you need to create a plugin using hook location init_start with this code:
PHP Code:
$datastore_fetch[]="'usercount'"

Then in a template where you want to display it add something like:
PHP Code:
<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.


Quote:
Originally Posted by vbdarwin View Post
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.
Reply With Quote
  #5  
Old 11-06-2010, 02:01 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You need to run that script as a cron job/scheduled task, not as a standalone file.
Reply With Quote
  #6  
Old 11-06-2010, 02:29 PM
vbdarwin vbdarwin is offline
 
Join Date: Nov 2010
Posts: 11
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Lynne View Post
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.
Reply With Quote
  #7  
Old 11-06-2010, 02:41 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 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.
Reply With Quote
  #8  
Old 11-06-2010, 02:52 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

[deleted this, see below instead]
Reply With Quote
  #9  
Old 11-06-2010, 08:08 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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).
Reply With Quote
  #10  
Old 11-06-2010, 08:52 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 05:02 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04617 seconds
  • Memory Usage 2,293KB
  • Queries Executed 12 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (4)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete