Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Adding Currently Active Users to your own vB page
Lynne's Avatar
Lynne
Join Date: Sep 2004
Posts: 41,180

 

California/Idaho
Show Printable Version Email this Page Subscription
Lynne Lynne is offline 08-05-2008, 10:00 PM

Adding Currently Active Users to your own vB page


I've seen a few users asking about how to add the list of Currently Active Users to their own vB page. Below I document the lines of code you must add after creating your own page using one of these articles - How to create your own vBulletin-powered page! (uses vB templates) or [How-To] vBulletin API Basics: Creating Custom Pages & Misc. This article only documents what to add to a working page in order to get your Currently Active Users. You should make sure your page is working first before adding this code. I'll refer to your working page as test.php and the template in your working page as 'TEST'.

Instructions

Open your working php page and ADD the maxloggedin template to the specialtemplates array (your maxloggedin users will get reset all the time if you don't add this - I learned that the hard way!):
PHP Code:
// get special data templates from the datastore
$specialtemplates = array(
    
'maxloggedin',
); 
ADD the forumhome_loggedinuser template to the globaltemplates array so it looks like this:
PHP Code:
// pre-cache templates used by all actions
$globaltemplates = array(
    
'TEST',
    
'forumhome_loggedinuser',
); 
Then ADD the functions_bigthree.php to the required files list so it looks like this:
PHP Code:
// ######################### REQUIRE BACK-END ############################
require_once('./global.php');
require_once(
DIR '/includes/functions_bigthree.php'); 
If you are creating this page outside of your normal vb forums directory, you should change directories prior to requiring these files. It would then look like this:
PHP Code:
// ######################### REQUIRE BACK-END ############################
chdir ('/path/to/your/forums');
require_once(
'./global.php');
require_once(
DIR '/includes/functions_bigthree.php'); 
Then open your index.php page and COPY the code between the following START and
END lines. Paste these lines into the main part of your page. (You might want to copy this code from a default index.php if you have modified your index.php page.)

START with:
PHP Code:
// ### LOGGED IN USERS #################################################
$activeusers '';
if ((
$vbulletin->options['displayloggedin'] == OR $vbulletin->options['displayloggedin'] == OR ($vbulletin->options['displayloggedin'] > AND $vbulletin->userinfo['userid'])) AND !$show['search_engine'])

END with:
PHP Code:
    $show['loggedinusers'] = true;
}
else
{
    
$show['loggedinusers'] = false;

Open your template that you made (TEST) and ADD the following code in the table. Make sure it is it's own new table row.
HTML Code:
<!-- logged-in users -->
<tbody>
    <tr>
        <td class="thead" colspan="2">
            <a style="float:$stylevar[right]" href="#top" onclick="return toggle_collapse('forumhome_activeusers')"><img id="collapseimg_forumhome_activeusers" src="$stylevar[imgdir_button]/collapse_thead$vbcollapse[collapseimg_forumhome_activeusers].gif" alt="" border="0" /></a>
            <a href="online.php$session[sessionurl_q]" rel="nofollow">$vbphrase[currently_active_users]</a>: $totalonline (<phrase 1="$numberregistered" 2="$numberguest">$vbphrase[x_members_and_y_guests]</phrase>)
        </td>
    </tr>
</tbody>
<tbody id="collapseobj_forumhome_activeusers" style="$vbcollapse[collapseobj_forumhome_activeusers]">
    <tr>
        <td class="alt2"><a href="online.php$session[sessionurl_q]" rel="nofollow"><img src="$stylevar[imgdir_misc]/whos_online.gif" alt="$vbphrase[view_whos_online]" border="0" /></a></td>
        <td class="alt1" width="100%">
            <div class="smallfont">
                <div style="white-space: nowrap"><phrase 1="$recordusers" 2="$recorddate" 3="$recordtime">$vbphrase[most_users_ever_online_was_x_y_at_z]</phrase></div>
                <div>$activeusers</div>
            </div>
        </td>
    </tr>
</tbody>
<!-- end logged-in users -->
Also, very important if you want your page to validate correctly!, change the other columns to span 2 columns. ie.
HTML Code:
<tr>
    <td class="tcat" colspan="2">Title</td>
</tr>
<tr>
    <td class="alt1" colspan="2">Text</td>
</tr>
That should do it! I tested this on my 3.6.8 board and on my 3.7.0 board.
Reply With Quote
  #32  
Old 11-15-2008, 03:59 AM
Forum Lover Forum Lover is offline
 
Join Date: Jun 2007
Location: Sydney
Posts: 503
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Roger that.
Reply With Quote
  #33  
Old 11-16-2008, 11:05 AM
djxcee djxcee is offline
 
Join Date: Mar 2008
Location: SoCal
Posts: 179
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by mokujin View Post
I have it, just add this code for your custom page.
After
Add:
PHP Code:
AND session.location LIKE '%your_script_name.php%' 
Thank you Lynne
How do I add more than one file?

Also, will this work on existing vBulletin files? Let's say I want to add it to Social Groups.
Reply With Quote
  #34  
Old 11-16-2008, 03:27 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by djxcee View Post
How do I add more than one file?

Also, will this work on existing vBulletin files? Let's say I want to add it to Social Groups.
The code in this tutorial is for a single page. So, if you want it on Social Groups, you add the code to that page and then add that line to the query with the social groups php page in there.
Reply With Quote
  #35  
Old 11-21-2008, 07:35 AM
eh9 daddy eh9 daddy is offline
 
Join Date: Feb 2007
Location: Germany
Posts: 21
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I tryed to add it on vb 3.8 beta2, but it don't work. it don't show any numbers, look at the screenshot. sorry for my bad english

here is my code from my php file

Code:
<?php

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('THIS_SCRIPT', 'test');


// get special phrase groups
$phrasegroups = array();

// get special data templates from the datastore
$specialtemplates = array(
	'maxloggedin',
);  

// pre-cache templates used by all actions
$globaltemplates = array(
	'Test',
	'forumhome_loggedinuser',
);



// ######################### REQUIRE BACK-END ############################
require_once('./global.php');
require_once(DIR . '/includes/functions_bigthree.php'); 

// #######################################################################
// ######################## START MAIN SCRIPT ############################
// #######################################################################

($hook = vBulletinHook::fetch_hook('startseite_main')) ? eval($hook) : false;


$navbits = array();
$navbits[$parent] = 'Test Page';

$navbits = construct_navbits($navbits);
eval('$navbar = "' . fetch_template('navbar') . '";');
eval('print_output("' . fetch_template('Test') . '");');

// ### LOGGED IN USERS #################################################
$activeusers = '';
if (($vbulletin->options['displayloggedin'] == 1 OR $vbulletin->options['displayloggedin'] == 2 OR ($vbulletin->options['displayloggedin'] > 2 AND $vbulletin->userinfo['userid'])) AND !$show['search_engine'])
{
	$datecut = TIMENOW - $vbulletin->options['cookietimeout'];
	$numbervisible = 0;
	$numberregistered = 0;
	$numberguest = 0;

	$hook_query_fields = $hook_query_joins = $hook_query_where = '';
	($hook = vBulletinHook::fetch_hook('forumhome_loggedinuser_query')) ? eval($hook) : false;

	$forumusers = $db->query_read_slave("
		SELECT
			user.username, (user.options & " . $vbulletin->bf_misc_useroptions['invisible'] . ") AS invisible, user.usergroupid,
			session.userid, session.inforum, session.lastactivity, session.badlocation,
			IF(displaygroupid=0, user.usergroupid, displaygroupid) AS displaygroupid, infractiongroupid
			$hook_query_fields
		FROM " . TABLE_PREFIX . "session AS session
		LEFT JOIN " . TABLE_PREFIX . "user AS user ON(user.userid = session.userid)
		$hook_query_joins
		WHERE session.lastactivity > $datecut
			$hook_query_where
		" . iif($vbulletin->options['displayloggedin'] == 1 OR $vbulletin->options['displayloggedin'] == 3, "ORDER BY username ASC") . "
	");

	if ($vbulletin->userinfo['userid'])
	{
		// fakes the user being online for an initial page view of index.php
		$vbulletin->userinfo['joingroupid'] = iif($vbulletin->userinfo['displaygroupid'], $vbulletin->userinfo['displaygroupid'], $vbulletin->userinfo['usergroupid']);
		$userinfos = array
		(
			$vbulletin->userinfo['userid'] => array
			(
				'userid'            =>& $vbulletin->userinfo['userid'],
				'username'          =>& $vbulletin->userinfo['username'],
				'invisible'         =>& $vbulletin->userinfo['invisible'],
				'inforum'           => 0,
				'lastactivity'      => TIMENOW,
				'usergroupid'       =>& $vbulletin->userinfo['usergroupid'],
				'displaygroupid'    =>& $vbulletin->userinfo['displaygroupid'],
				'infractiongroupid' =>& $vbulletin->userinfo['infractiongroupid'],
			)
		);
	}
	else
	{
		$userinfos = array();
	}
	$inforum = array();

	while ($loggedin = $db->fetch_array($forumusers))
	{
		if ($loggedin['badlocation'])
		{
			continue;
		}

		$userid = $loggedin['userid'];
		if (!$userid)
		{	// Guest
			$numberguest++;
			$inforum["$loggedin[inforum]"]++;
		}
		else if (empty($userinfos["$userid"]) OR ($userinfos["$userid"]['lastactivity'] < $loggedin['lastactivity']))
		{
			$userinfos["$userid"] = $loggedin;
		}
	}

	if (!$vbulletin->userinfo['userid'] AND $numberguest == 0)
	{
		$numberguest++;
	}

	foreach ($userinfos AS $userid => $loggedin)
	{
		$numberregistered++;
		if ($userid != $vbulletin->userinfo['userid'])
		{
			$inforum["$loggedin[inforum]"]++;
		}
		fetch_musername($loggedin);

		($hook = vBulletinHook::fetch_hook('forumhome_loggedinuser')) ? eval($hook) : false;

		if (fetch_online_status($loggedin))
		{
			$numbervisible++;
			$show['comma_leader'] = ($activeusers != '');
			eval('$activeusers .= "' . fetch_template('forumhome_loggedinuser') . '";');
		}
	}

	// memory saving
	unset($userinfos, $loggedin);

	$db->free_result($forumusers);

	$totalonline = $numberregistered + $numberguest;
	$numberinvisible = $numberregistered - $numbervisible;

	// ### MAX LOGGEDIN USERS ################################
	if (intval($vbulletin->maxloggedin['maxonline']) <= $totalonline)
	{
		$vbulletin->maxloggedin['maxonline'] = $totalonline;
		$vbulletin->maxloggedin['maxonlinedate'] = TIMENOW;
		build_datastore('maxloggedin', serialize($vbulletin->maxloggedin), 1);
	}

	$recordusers = vb_number_format($vbulletin->maxloggedin['maxonline']);
	$recorddate = vbdate($vbulletin->options['dateformat'], $vbulletin->maxloggedin['maxonlinedate'], true);
	$recordtime = vbdate($vbulletin->options['timeformat'], $vbulletin->maxloggedin['maxonlinedate']);

	$show['loggedinusers'] = true;
}
else
{
	$show['loggedinusers'] = false;
}

?>
and my code from the template

Code:
<!-- logged-in users -->
<table>
	<tr>
		<td class="thead" colspan="2">
			<a style="float:$stylevar[right]" href="#top" onclick="return toggle_collapse('forumhome_activeusers')"><img id="collapseimg_forumhome_activeusers" src="$stylevar[imgdir_button]/collapse_thead$vbcollapse[collapseimg_forumhome_activeusers].gif" alt="" border="0" /></a>
			<a href="online.php$session[sessionurl_q]">$vbphrase[currently_active_users]</a>: $totalonline (<phrase 1="$numberregistered" 2="$numberguest">$vbphrase[x_members_and_y_guests]</phrase>)
		</td>
	</tr>
</table>
<table id="collapseobj_forumhome_activeusers" style="$vbcollapse[collapseobj_forumhome_activeusers]">
	<tr>
		<td class="alt2"><a href="online.php$session[sessionurl_q]"><img src="$stylevar[imgdir_misc]/whos_online.gif" alt="$vbphrase[view_whos_online]" border="0" /></a></td>
		<td class="alt1" width="100%">
			<div class="smallfont">
				<div style="white-space: nowrap"><phrase 1="$recordusers" 2="$recorddate" 3="$recordtime">$vbphrase[most_users_ever_online_was_x_y_at_z]</phrase></div>
				<div>$activeusers</div>
			</div>
		</td>
	</tr>
</table>
<!-- end logged-in users -->
Attached Images
File Type: jpg wol.jpg (22.7 KB, 0 views)
Reply With Quote
  #36  
Old 11-21-2008, 03:02 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

This was written to be used in conjunction with How to create your own vBulletin-powered page! (uses vB templates) or [How-To] vBulletin API Basics: Creating Custom Pages & Misc. You need to eval/fetch your templates after the code like in those articles. This article is NOT stand alone code. You MUST use it with an already working vb powered page.
Reply With Quote
  #37  
Old 11-21-2008, 03:29 PM
eh9 daddy eh9 daddy is offline
 
Join Date: Feb 2007
Location: Germany
Posts: 21
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

i have an working vbulletin powered page. in this page i use for example the vbulletin login, recent forum threads, latest album pictures and so on. everything works in this page, only who's online works not.
Reply With Quote
  #38  
Old 11-21-2008, 03:55 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Paste these lines into the main part of your page.
You pasted it at the end of your script. You need to put it before you eval your templates - right under "START MAIN SCRIPT", before the $navbits stuff.
Reply With Quote
  #39  
Old 11-21-2008, 04:03 PM
eh9 daddy eh9 daddy is offline
 
Join Date: Feb 2007
Location: Germany
Posts: 21
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thanks alot, this was the error
Reply With Quote
  #40  
Old 12-27-2008, 07:19 PM
shoolace shoolace is offline
 
Join Date: Nov 2008
Posts: 4
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you for this article. It explains everything perfectly. Installed in no time.

Voted 5 stars. :up:

Shoolace
Reply With Quote
  #41  
Old 02-20-2009, 05:47 PM
Submerge Submerge is offline
 
Join Date: Jan 2006
Posts: 29
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Question;

I have the ability to log in users on all my non-VB pages. If a user comes on a non-VB page, it'll fetch their cookies and automatically log them in if they are a member who selected the Remember Me option. Otherwise it'll act as if they are a Guest user.

I don't want to display the Currently Active Users on my non-VB pages, but I'd like to know if there are users on my non-VB pages by looking at the Currently Active Users on the forumhome.

Do all I need to add to do this then is this?
Code:
// ######################### REQUIRE BACK-END ############################
chdir ('/path/to/your/forums');
require_once('./global.php');
require_once(DIR . '/includes/functions_bigthree.php');
That way Ill know how many vBulletin members are on at a certain time throughout my entire website, not just the forums? (Since I have that code in the header.php file that all non-VB pages use)

[edit]
I tried this out but it's not recognizing users on my site's index.php (which uses the header.php file I'm putting the code into.

Here are the first lines that the header.php file is using to try and get this working:
Code:
<?php
	//vBulletin Member Recognition
	$curdir = getcwd ();
	chdir('forums/');
	require_once('forums/global.php');
	require_once(DIR . '/includes/functions_bigthree.php'); //Fetch current active users?
	chdir ($curdir);
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 12:58 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.06129 seconds
  • Memory Usage 2,371KB
  • Queries Executed 26 (?)
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
  • (4)bbcode_code
  • (2)bbcode_html
  • (7)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (3)pagenav_pagelink
  • (11)post_thanks_box
  • (11)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (11)post_thanks_postbit_info
  • (10)postbit
  • (1)postbit_attachment
  • (11)postbit_onlinestatus
  • (11)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
  • postbit_attachment
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete