News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Get users viewing a page with SSI

Started by torcado194, August 17, 2014, 06:35:20 PM

Previous topic - Next topic

torcado194

I have been using SSI to create my own chat on my forum, and i would like to integrate a "users in chat" feature.

Is there any way to get the names of users currently viewing a page with SSI?

I know theres settings and mods for users viewing boards and whatnot, but nothing was really what i was looking for.

thanks for your help!

Kindred

I don't think it can be easily done...
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

torcado194

#2
Alright..

The only other thoughts I had about achieving this would be:
1. Get the 50 most recent chat lines' senders and display them as "in chat"
2. Add an entry "date" to chat lines and pull users who chatted in the past x minutes
3. Create a new table and send usernames with timestamps every x seconds and check entries in table with timestamps in the last x seconds

Ill probably go with option 1 for now because it's simple and I don't feel like learning how to set up a timestamp feature for now, though ill probably set that up later on anyway because it's useful to see when chats were sent.

onepiece

Add the following two lines of code after you require SSI.php file in your chat SSI script:

$_GET['action'] = 'chat';
writeLog();


Then you can use a query like the following one to fetch the list of users viewing the page:

global $smcFunc, $scripturl;

$request = $smcFunc['db_query']('', '
SELECT lo.id_member, mem.real_name
FROM {db_prefix}log_online AS lo
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lo.id_member)
WHERE INSTR(lo.url, {string:in_url_string}) > 0
AND lo.id_member > {int:empty}',
array(
'in_url_string' => 's:6:"action";s:4:"chat"',
'empty' => 0,
)
);
$users = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$users[$row['id_member']] = $row['real_name'];
$smcFunc['db_free_result']($request);

foreach ($users as $id => $username)
echo '<a href="', $scripturl, '?action=profile;u=', $id, '">', $username, '</a><br />';

torcado194

thanks a lot for this! it works almost perfectly.

There is a problem though:

if someone just closes the page, they are still shown as "on the page", they have to actually go to another page on the forum before it recognises that they arent on the page. i assume this is because it only checks the last page their account has been

Arantor

Yup, closing the tab won't send any notification to SMF that they're leaving the page. You could rig something up with AJAX but even that won't be foolproof.

Incidentally the same is true the rest of the time in SMF too.

torcado194

alright thanks for the info. ill probably just go with my 2nd option then because even yet that code was giving me some problems.

dougiefresh

Thank you for figuring this out.  I was looking for something like this!

Quote from: torcado194 on August 18, 2014, 08:11:33 PM
if someone just closes the page, they are still shown as "on the page", they have to actually go to another page on the forum before it recognises that they arent on the page. i assume this is because it only checks the last page their account has been
Looks like the query needs to be time-restricted as well....  The query probably needs to be something like this:
$request = $smcFunc['db_query']('', '
SELECT lo.id_member, mem.real_name
FROM {db_prefix}log_online AS lo
LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = lo.id_member)
WHERE INSTR(lo.url, {string:in_url_string}) > 0
AND lo.id_member > {int:empty}
AND lo.log_time > {int:minimum_time}',
array(
'in_url_string' => 's:6:"action";s:4:"chat"',
'empty' => 0,
'minimum_time' => time() - 900,
)
);

This modified query limits the search to the last 15 minutes....  Hope this helps someone....

Advertisement: