MyBB Community Forums

Full Version: Board Statistics on another page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

How to I add board statistics to a custom php page?
You can achieve this with adding $cache->read("stats"); and manipulating variables.
Copy the stats.php file to a location inside or outside your MyBB root, rename if you copy it inside.
Define the absolute path for the MyBB root:
define('THIS_SCRIPT', 'stats.php');
define('MYBB_ROOT', '/path/to/mybb');

Now give in an relative path to your global.php file if your working outside the MyBB root:
Search:
require_once "./global.php";
Replace with:
require_once "../mybb/global.php";

And instead of print the template you can access the stat array yourself and do whatever you want with it:
Search for:
eval("\$stats = \"".$templates->get("stats")."\";");
output_page($stats);
Replace with:
var_dump($stats);
Sorry, I should have specified a little more...

What I meant by the board statistics was the "how many users online" with their username showing just like on the forum index. Sorry... Confused
Try this:
<?php
define("IN_MYBB", 1);
define('THIS_SCRIPT', 'stats.php');
define('MYBB_ROOT', '/path/to/mybb/');
define('NO_ONLINE', true);

$templatelist = "stats,stats_thread";
require_once "../relpath/to/1603/global.php";

// Get the online users.
$timesearch = TIME_NOW - $mybb->settings['wolcutoff'];
$comma = '';
$query = $db->query("
	SELECT s.sid, s.ip, s.uid, s.time, s.location, s.location1, u.username, u.invisible, u.usergroup, u.displaygroup
	FROM ".TABLE_PREFIX."sessions s
	LEFT JOIN ".TABLE_PREFIX."users u ON (s.uid=u.uid)
	WHERE s.time>'$timesearch'
	ORDER BY u.username ASC, s.time DESC
");

$membercount = 0;
$guestcount = 0;
$botcount = 0;
$anoncount = 0;
$doneusers = array();

// Fetch spiders
$spiders = $cache->read("spiders");

// Loop through all users.
while($user = $db->fetch_array($query))
{
	
	// Create a key to test if this user is a search bot.
	$botkey = my_strtolower(str_replace("bot=", '', $user['sid']));

	// Decide what type of user we are dealing with.
	if($user['uid'] > 0)
	{
		// The user is registered.
		if($doneusers[$user['uid']] < $user['time'] || !$doneusers[$user['uid']])
		{
			// If the user is logged in anonymously, update the count for that.
			if($user['invisible'] == 1)
			{
				++$anoncount;
			}
			++$membercount;
			if($user['invisible'] != 1 || $mybb->usergroup['canviewwolinvis'] == 1 || $user['uid'] == $mybb->user['uid'])
			{
				// If this usergroup can see anonymously logged-in users, mark them.
				if($user['invisible'] == 1)
				{
					$invisiblemark = "*";
				}
				else
				{
					$invisiblemark = '';
				}

				// Properly format the username and assign the template.
				$user['username'] = format_name($user['username'], $user['usergroup'], $user['displaygroup']);
				$user['profilelink'] = build_profile_link($user['username'], $user['uid']);
			}
			// This user has been handled.
			$doneusers[$user['uid']] = $user['time'];
		}
	}
	elseif(my_strpos($user['sid'], "bot=") !== false && $spiders[$botkey])
	{
		$user['username'] = format_name($spiders[$botkey]['name'], $spiders[$botkey]['usergroup']);
		++$botcount;
	}
	else
	{
		// The user is a guest.
		++$guestcount;
	}
	
	$online_users[] = $user;
}

var_dump($online_users);
?>

It's a slightly modified snippet from the index.php file. The $online_users array holds all the information (even the location browsing etc) and you can use $membercount, $guestcount and $botcount to show the counts.

[EDIT]
PHP tags always screw up the layout so used code tags.
I have changed the necessary paths to the files required and I land on a server error page.
<?php
define("IN_MYBB", 1);
define('THIS_SCRIPT', 'stats.php');
define('MYBB_ROOT', '/mybb/');
define('NO_ONLINE', true);

$templatelist = "stats,stats_thread";
require_once "../mybb/global.php";

// Get the online users.
$timesearch = TIME_NOW - $mybb->settings['wolcutoff'];
$comma = '';
$query = $db->query("
    SELECT s.sid, s.ip, s.uid, s.time, s.location, s.location1, u.username, u.invisible, u.usergroup, u.displaygroup
    FROM ".TABLE_PREFIX."sessions s
    LEFT JOIN ".TABLE_PREFIX."users u ON (s.uid=u.uid)
    WHERE s.time>'$timesearch'
    ORDER BY u.username ASC, s.time DESC
");

$membercount = 0;
$guestcount = 0;
$botcount = 0;
$anoncount = 0;
$doneusers = array();

// Fetch spiders
$spiders = $cache->read("spiders");

// Loop through all users.
while($user = $db->fetch_array($query))
{
    
    // Create a key to test if this user is a search bot.
    $botkey = my_strtolower(str_replace("bot=", '', $user['sid']));

    // Decide what type of user we are dealing with.
    if($user['uid'] > 0)
    {
        // The user is registered.
        if($doneusers[$user['uid']] < $user['time'] || !$doneusers[$user['uid']])
        {
            // If the user is logged in anonymously, update the count for that.
            if($user['invisible'] == 1)
            {
                ++$anoncount;
            }
            ++$membercount;
            if($user['invisible'] != 1 || $mybb->usergroup['canviewwolinvis'] == 1 || $user['uid'] == $mybb->user['uid'])
            {
                // If this usergroup can see anonymously logged-in users, mark them.
                if($user['invisible'] == 1)
                {
                    $invisiblemark = "*";
                }
                else
                {
                    $invisiblemark = '';
                }

                // Properly format the username and assign the template.
                $user['username'] = format_name($user['username'], $user['usergroup'], $user['displaygroup']);
                $user['profilelink'] = build_profile_link($user['username'], $user['uid']);
            }
            // This user has been handled.
            $doneusers[$user['uid']] = $user['time'];
        }
    }
    elseif(my_strpos($user['sid'], "bot=") !== false && $spiders[$botkey])
    {
        $user['username'] = format_name($spiders[$botkey]['name'], $spiders[$botkey]['usergroup']);
        ++$botcount;
    }
    else
    {
        // The user is a guest.
        ++$guestcount;
    }
    
    $online_users[] = $user;
}

var_dump($online_users);
?>
The MYBB_ROOT needs to be the absolute path starting from the Linux root (/) or (if on Windows) a path like C:\www\... . You can also use realpath() to get the absolute path from a relative path.
But first you need to find the good relative path. Lets say you have this structure:
mybb/global.php
yourscript.php
The correct relative path in your script is "./mybb/"

If you have structure like this:
mybb/global.php
dir/yourscript.php
The correct relative path is: ../mybb/

.= the current directory
..= directory up

To test if you have a good relative, do this from your script:
var_dump(realpath("./mybb/"));
If you get a path, it's correct. If you get false, it's incorrect.

If you have the correct relative path and it is "./mybb/", fix those lines:
// ...
define('MYBB_ROOT', realpath('./mybb/'));
// ...
require_once "./mybb/global.php";