MyBB Community Forums

Full Version: How to check and see Who's online on a custom page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi

i recently created an ajax chatbox for my forum so it has a custom page called chat.php
so i want to check all the users who are on this page chat.php to see whos chatting and things like that
i know this is possible because mybb has whos online page and tells you where are the members at on online.php
so i want to check whos online at my custom page is that possible ?
It will require that the sessions table for the guests in the chat is updated properly.

The coding how the information of visitors is gathered can be found in scripts like forumdisplay.php, showthread.php etc. Look for "online" in these scripts and you will find it (a query that searches in the sessions table followed by the coding to split them up in members, guests and invisibles).

E.g.
forumdisplay.php lines 274 - 357
showthread.php lines 1441 - 1520

Off course, the query must be changed such that the users visiting the chat are returned.

The result is the variable $usersbrowsing which contains all information and can be placed in your template.


EDIT
Some errors in the above data corrected
So mybb uses sessions to gather up online users ?
Yes, this is part of general.php. When you have included general.php in the start of your chat.php, so that this starts with:

define("IN_MYBB", 1);
define('THIS_SCRIPT', 'chat.php');
require_once "./global.php";

then a session is started when a user enters the chat, and the sessions table of the database is extended or updated with a row for that user which also contains the data of the script the user is using.
I dont know whats wrong with my code for some reason its not showing anything this is what i got:

// Get the users browsing this forum.
if($mybb->settings['browsingthisforum'] != 0)
{
	$timecut = TIME_NOW - $mybb->settings['wolcutoff'];

	$comma = '';
	$guestcount = 0;
	$membercount = 0;
	$inviscount = 0;
	$onlinemembers = '';
	$doneusers = array();

	$query = $db->query("
		SELECT s.ip, s.uid, u.username, s.time, u.invisible, u.usergroup, u.usergroup, u.displaygroup
		FROM ".TABLE_PREFIX."sessions s
		LEFT JOIN ".TABLE_PREFIX."users u ON (s.uid=u.uid)
		WHERE s.time > '$timecut' AND location1='/yana/chat.php?' AND nopermission != 1
		ORDER BY u.username ASC, s.time DESC
	");

	while($user = $db->fetch_array($query))
	{
		if($user['uid'] == 0)
		{
			++$guestcount;
		}
		else
		{
			if(empty($doneusers[$user['uid']]) || $doneusers[$user['uid']] < $user['time'])
			{
				$doneusers[$user['uid']] = $user['time'];
				++$membercount;
				if($user['invisible'] == 1)
				{
					$invisiblemark = "*";
					++$inviscount;
				}
				else
				{
					$invisiblemark = '';
				}

				if($user['invisible'] != 1 || $mybb->usergroup['canviewwolinvis'] == 1 || $user['uid'] == $mybb->user['uid'])
				{
					$user['username'] = format_name($user['username'], $user['usergroup'], $user['displaygroup']);
					$user['profilelink'] = build_profile_link($user['username'], $user['uid']);
					eval("\$onlinemembers .= \"".$templates->get("forumdisplay_usersbrowsing_user", 1, 0)."\";");
					$comma = $lang->comma;
				}
			}
		}
	}

	$guestsonline = '';
	if($guestcount)
	{
		$guestsonline = $lang->sprintf($lang->users_browsing_forum_guests, $guestcount);
	}

	$invisonline = '';
	if($mybb->user['invisible'] == 1)
	{
		// the user was counted as invisible user --> correct the inviscount
		$inviscount -= 1;
	}
	if($inviscount && $mybb->usergroup['canviewwolinvis'] != 1)
	{
		$invisonline = $lang->sprintf($lang->users_browsing_forum_invis, $inviscount);
	} 
	

	$onlinesep = '';
	if($invisonline != '' && $onlinemembers)
	{
		$onlinesep = $lang->comma;
	}
	
	$onlinesep2 = '';
	if($invisonline != '' && $guestcount || $onlinemembers && $guestcount)
	{
		$onlinesep2 = $lang->comma;
	}
}


but when i try to echo it out nothing appears
This is wrong in your query:

location1='/yana/chat.php?'


This should be:

location='/yana/chat.php?'

location1 (and location2) are only used for forumdisplay.php and showthread.php.

Also:

eval("\$usersbrowsing = \"".$templates->get("showthread_usersbrowsing")."\";");


at the end is missing
It didnt work this is my whole document 

<?php
Define("DB_HOST", "localhost") ;
Define("DB_USER", "root") ;
Define("DB_PWD", "") ;
Define("DB_NAME", "mybb") ;

$conn = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_NAME);

if(mysqli_connect_errno()){
	die("هەڵە ڕوویدا");
}

?>
<?php
define('IN_MYBB', 1); // (1a)
require "./global.php"; // (1b)
global $mybb;



// Get the users browsing this forum.
if($mybb->settings['browsingthisforum'] != 0)
{
    $timecut = TIME_NOW - $mybb->settings['wolcutoff'];

    $comma = '';
    $guestcount = 0;
    $membercount = 0;
    $inviscount = 0;
    $onlinemembers = '';
    $doneusers = array();

    $query = $db->query("
        SELECT s.ip, s.uid, u.username, s.time, u.invisible, u.usergroup, u.usergroup, u.displaygroup
        FROM ".TABLE_PREFIX."sessions s
        LEFT JOIN ".TABLE_PREFIX."users u ON (s.uid=u.uid)
        WHERE s.time > '$timecut' AND location='/yana/chat.php?' AND nopermission != 1
        ORDER BY u.username ASC, s.time DESC
    ");

    while($user = $db->fetch_array($query))
    {
        if($user['uid'] == 0)
        {
            ++$guestcount;
        }
        else
        {
            if(empty($doneusers[$user['uid']]) || $doneusers[$user['uid']] < $user['time'])
            {
                $doneusers[$user['uid']] = $user['time'];
                ++$membercount;
                if($user['invisible'] == 1)
                {
                    $invisiblemark = "*";
                    ++$inviscount;
                }
                else
                {
                    $invisiblemark = '';
                }

                if($user['invisible'] != 1 || $mybb->usergroup['canviewwolinvis'] == 1 || $user['uid'] == $mybb->user['uid'])
                {
                    $user['username'] = format_name($user['username'], $user['usergroup'], $user['displaygroup']);
                    $user['profilelink'] = build_profile_link($user['username'], $user['uid']);
                    eval("\$onlinemembers .= \"".$templates->get("forumdisplay_usersbrowsing_user", 1, 0)."\";");
                    $comma = $lang->comma;
                }
            }
        }
    }

    $guestsonline = '';
    if($guestcount)
    {
        $guestsonline = $lang->sprintf($lang->users_browsing_forum_guests, $guestcount);
    }

    $invisonline = '';
    if($mybb->user['invisible'] == 1)
    {
        // the user was counted as invisible user --> correct the inviscount
        $inviscount -= 1;
    }
    if($inviscount && $mybb->usergroup['canviewwolinvis'] != 1)
    {
        $invisonline = $lang->sprintf($lang->users_browsing_forum_invis, $inviscount);
    } 
    

    $onlinesep = '';
    if($invisonline != '' && $onlinemembers)
    {
        $onlinesep = $lang->comma;
    }
    
    $onlinesep2 = '';
    if($invisonline != '' && $guestcount || $onlinemembers && $guestcount)
    {
        $onlinesep2 = $lang->comma;
    }
} 


if($mybb->user['uid']) {
	$naw = "<span id=\"naw_bakarhenar\">" . format_name($mybb->user['username'], $mybb->user['usergroup'], $mybb->user['displaygroup']) . "</span>" ;
	$nama= "";
} else {
	$tekal = rand(10, 1000);
	$naw = "<span id=\"naw_bakarhenar\">میوان({$tekal})</span>";
	$nama= ", ئەگەر لەیانەی زانیاری ئەندامی <a href=\"http://localhost/yana/member.php?action=login\">كرتە بكە</a> بۆ چوونەژوورەوە ئەگەر ئەندامیش نیت <a href=\"http://localhost/yana/member.php?action=register\">كرتە بكە</a> بۆ خۆتۆماركردن";
}
$baxerbee = 'بەخێربێی ' . $naw . $nama;

//7alate sarhel
$lahelm = "INSERT INTO";

//7alate sarhel tawaw


$henan_chat = "SELECT * FROM (
    SELECT * FROM mybb_chat ORDER BY id DESC LIMIT 15
) sub
ORDER BY id ASC";
$henan_chatset = mysqli_query($conn, $henan_chat);
?>
<?php 
add_breadcrumb("چاتی زانیاری", "chat.php"); 

$template = '
<!DOCTYPE html>
<html>
<head>
<title>چاتی زانیاری</title>
<script src="./jscripts/chat/jquery.js"></script>
<script src="./jscripts/chat/gshte.js"></script>
{$headerinclude}
</head>
<body>
{$header}
	
	
	
<table border="0" cellspacing="0" cellpadding="5" class="tborder">
<thead>
<tr>
<td class="thead" colspan="5">
<div class="expcolimage"><img src="http://localhost/yana/images/hastanawa/collapse.png" id="cat_1_img" class="expander" alt="[-]" title="[-]" style="cursor: pointer;"></div>
<div><strong>چاتی زانیاری</strong><br><div class="smalltext"></div></div>
</td>
</tr>
</thead>
<tbody id="cat_1_e">
<tr>
	<td class="tcat" width="818" style="display:inline-block;" ><span class="smalltext"><strong>یانە</strong></span></td>
	<td class="tcat" width="350" style="display:inline-block; float: left;" align="center"><span class="smalltext"><strong>ئامادەبووانی چات</strong></span>		</td>
</tr>
<tr>
		<td width="100%">
			{$baxerbee}<br .><div id="chat_box"><div id="chat_inner">' ;
			if($henan_chatset && mysqli_affected_rows($conn) >= 1) {
				while($anjam = mysqli_fetch_assoc($henan_chatset)) {
					$template .=  "<span class=\"chat_text\" data-id=\"" . $anjam["id"] . "\"><strong>" . $anjam["ner"] . "</strong>: " ;
					$template .= htmlentities($anjam["nawarok"]) . "</span><br />" ;
				}
			} else {
				$template .= "هیچ گفتوگۆیێك نەدزرایەوە";
			}  
$template .= '</div><div id="nama_stat"></div><input type="text" id="chat_text" placeholder="گفتوگۆ بكە لەگەڵ ئەندامانی یانەی زانیاری" value="">
<select id="xanda">
<option>خەندە</option>
<option id="x_1">:)</option>
<option id="x_2">D:</option>
<option id="x_3">:(</option>
</select>
<button type="submit" id="chat_enter">ناردن</button>
</div>
</td>
</tr>
<tr><td>{$usersbrowsing}</td></tr>
</tbody>
</table>	
	
	
{$footer}
</body>
</html>';

$template = str_replace("\'", "'", addslashes($template));
eval("\$testpage=\"".$template."\";");
output_page($testpage);
?>
See my last (added) remark, the eval statement is missing.
But i dont use mybb templates i injected the template inside the php file !
No, you added {$usersbrowsing} into your template, but as it is now $usersbrowsing is not evaluated because the eval is not executed.
Then you should have included the complete showthread_usersbrowsing template into your template.
Pages: 1 2