Gossamer Forum
Home : Products : Gossamer Forum : Development, Plugins and Globals :

How to - Display list of users online via SSI

Quote Reply
How to - Display list of users online via SSI
Instructions on how to display a list of members / guests currently viewing the forum
on any webpage via SSI (tested on .shtml pages)

Example (with visitors): Viewing the Forum: Pete, John, Dave and 15 guests

Example (without visitors): Viewing the Forum: No members and no guests

------------------------------

1. Create 2 globals:

(Admin/Templates/Global Vars)

Code:
TAG: online_now

GLOBAL:

sub {
my @found = ();
$DB->table('Online')->delete(GT::SQL::Condition->new(online_time => '<' => (time - $CFG->{online_timeout} * 60)));
my $table = $DB->table('Online','User');
my $sth = $table->select( { 'Online.online_invisible' => 0, 'User.user_invisible' => 0, 'Online.guest_id_fk' => 0 }, ['User.user_username'] );
while (my $userid = $sth->fetchrow_hashref){
push @found, $userid->{user_username};
}
return '' . (@found ? join(', ', @found) : 'No members');
}
Code:
TAG: online_guest

GLOBAL:

sub {
return $DB->table('Guest')->count( GT::SQL::Condition->new('guest_last_time', '>', time - 900) );
}

2. Create a template:

(Admin/Templates/User Templates)

To do this I opened .tplinfo and replaced the contents with the following then below where
it displays "Save template as:" rename it to ssi_online.html and click Save.


Code:
<html>
<head>
<%include include_css.html%>
</head>
<body bgcolor="<%light_green%>">
Viewing the Forums:</a> <%online_now%> and <%online_guest%> guests</font>
</body>
</html>
(This is just a basic code. You can change it to whatever font, color, etc. to match your site theme.)


3. Create an Action

(Admin/Setup/Actions)

Add Action - ssi_online
Description - ssi online (or whatever you want to call it)
Enabled - Yes
Page - ssi_online.html


To call it via SSI use the following (change to your path):

Code:
<!--#include virtual="/cgi-bin/forum/gforum.cgi?do=ssi_online"-->
Quote Reply
Re: [MJB] How to - Display list of users online via SSI In reply to
Can admin move this post to "Products: Gossamer Forum: Development, Plugins and Globals:" please.

Thanks
Quote Reply
Re: [MJB] How to - Display list of users online via SSI In reply to
Moved Smile

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [MJB] How to - Display list of users online via SSI In reply to
This worked perfectly. Very easy to understand too. Thank you!

I saw on another forum where each viewers name was 'click-able' to direct you to their Profile page. Can the code be revised to accomodate this feature?
Quote Reply
Re: [inboxcom] How to - Display list of users online via SSI In reply to
You need to change:

Code:
push @found, $userid->{user_username};
}
return '' . (@found ? join(', ', @found) : 'No members');

to

Code:
push @found, "<a href=\"/cgi-bin/gforum/gforum.cgi?username=$userid->{user_username}\">$userid->{user_username}</a>";
}
return '' . (@found ? \join(', ', @found) : 'No members');

Change the URL to your forum address.

Note: I have tried this on my server but it just returns a SCALAR error. Frown Give it a try and see if it works for you.
Quote Reply
Re: [MJB] How to - Display list of users online via SSI In reply to
I got the error too: SCALAR(0x3c94600)

Don't give in... you can solve this!!
Quote Reply
Re: [inboxcom] How to - Display list of users online via SSI In reply to
OK, I think I've got it.

Do as before above and then change the last line from:

Code:
return '' . (@found ? join(', ', @found) : 'No members');

to:

Code:
return (@found ? \join(', ', @found) : 'No members');

( Remove ' ' . and add \ )
Quote Reply
Re: [MJB] How to - Display list of users online via SSI In reply to
Yep, that works. Nice job!!!
Quote Reply
Re: [inboxcom] How to - Display list of users online via SSI In reply to
Here's the full global for anyone else who want's to use the adapted version.

If you want the displayed usernames to be hyperlinks to their user profile pages replace the original global with the following:

Code:
sub { # Last 15 minutes
my @found = ();
$DB->table('Online')->delete(GT::SQL::Condition->new(online_time => '<' => (time - $CFG->{online_timeout} * 60)));
my $table = $DB->table('Online','User');
my $sth = $table->select( { 'Online.online_invisible' => 0, 'User.user_invisible' => 0, 'Online.guest_id_fk' => 0 }, ['User.user_username'] );
while (my $userid = $sth->fetchrow_hashref){
push @found, "<a href=\"/cgi-bin/gforum/gforum.cgi?username=$userid->{user_username}\">$userid->{user_username}</a>";
}
return (@found ? \join(', ', @found) : 'No members');
}

and edit the "a href" link to your GForum URL.
Quote Reply
Re: [MJB] How to - Display list of users online via SSI In reply to
I noticed that the online_guest global never displayed the same amount of guests as the Who's Online page. Also, when using it with SSI, the page that it is displayed on counts visitors when they access that page and are not on the forum.

To fix this replace the global:

Code:
sub {
return $DB->table('Guest')->count(GT::SQL::Condition->new('guest_last_time', '>', time - 900));
}

with:

Code:
sub {
return $DB->table('Online')->count(GT::SQL::Condition->new(guest_id_fk => '<' => (time - $CFG->{online_timeout} * 60)));
}

Last edited by:

MJB: Jul 21, 2006, 2:55 PM