Online Members on External Page returning 0

LPH

Well-known member
Here is the code:

PHP:
$dependencies = new XenForo_Dependencies_Public();

$dependencies->preLoadData();

XenForo_Template_Public::setStyleId(1);

XenForo_Template_Public::setLanguageId(1);

$params = $dependencies->getEffectiveContainerParams(array(), new Zend_Controller_Request_Http());

$template =$dependencies->createTemplateObject('sidebar_online_users', $params);

echo $template->render();

The code returns

Code:
Members Online Now
Total: 0 (members: 0, guests: 0, robots: 0)

What am I doing wrong to not get the number of members, etc returned?
 
You are fetching the container params. These are the params that are used outside of the content, eg headers, nav tabs etc.

The params aren't going to just be available somewhere. Instead you are going to need to inspect the existing code, see how those params are got and then pass them to the template.

Everything else is correct. It's just a case of putting the right stuff in $params
 
As always, Thank you.

PS. I searched here yesterday several times and found nothing. I looked this morning and found multiple threads on this same topic. Oops.
 
Last edited:
@Chris D et al.

Sorry - but I have more dumb questions. Chris set me in the right direction by looking at params and I also realized that the session was not in the code. This has now been added and the code almost works. Sadly, the links are all wrong. They are not actually going to the /community/members but to /members ...

Is there a good way to fix the links so that they would go to the proper places? Does this mean extending a model in XenForo?

Here is my current code (if this helps others):

PHP:
$dependencies = new XenForo_Dependencies_Public();
$dependencies->preLoadData();XenForo_Template_Public::setStyleId(1);
XenForo_Template_Public::setLanguageId(1);
/** * Start session  ( the $XF is from XenWord ) */
$sessionModel = $XF->getModelFromCache('XenForo_Model_Session');
$visitor = XenForo_Visitor::getInstance();
/** * Pull onlineUsers */
$onlineUsers = $sessionModel->getSessionActivityQuickList($visitor->toArray(), array('cutOff' => array('>',$sessionModel->getOnlineStatusTimeout())), ($visitor['user_id'] ? $visitor->toArray() : null));
$params = array('onlineUsers' => $onlineUsers,'visitor' => $visitor);
$template =$dependencies->createTemplateObject('sidebar_online_users', $params);
echo $template->render();

Screen Shot 2014-04-15 at 12.41.18 PM.webp
 
Back
Top Bottom