Author Topic: Snippet: Display members online  (Read 17384 times)

Offline MoSaG

  • Posts: 15
Snippet: Display members online
« on: August 27, 2010, 06:51:03 PM »
My first snippet (and first module for WB in any way) - and I am not a pro-php-developer ;)

The reason for it was to learn, how to write code for WB modules.

It displays all usernames of logged-in users in an unordered list.

Put
Code: [Select]
<?php MembersOnline(); ?>to your template, thats all.

Optional Parameters:
  • (1) seconds of timeout
  • (2) custom headline
  • (3) selection of display: username or display_name
  • (4) custom 'no users online' message

i.e.
Code: [Select]
<?php MembersOnline(&#39;600&#39;, &#39;users online&#39;, &#39;username&#39;, &#39;-&#39;); ?>
default is
Code: [Select]
<?php MembersOnline(&#39;300&#39;, &#39;Active users in last 5 min&#39;, &#39;display_name&#39;, &#39;0 users online&#39;); ?>

You can modify the appereance by editing the frontend.htt of the snippet.

Hope it is bugfree, please report any errors, suggestions or security issues, 'will try to fix them.

Perhaps there are useful additions like a linked username where you can directly email the user ... or something like that. But this is my first try and I need more learning ;)

Sorry for my english ...

Updated to 0.1.1: added support for a custom timeout; simply add seconds for timeout in the brackets
Updated to 0.1.2: added support for a custom headline, add headline in quotes and separated from seconds to the brackets
Updated to 0.1.3: added support for custom selection of username or display_name, add 'username' or 'display_name' as third parameter
Updated to 0.1.4: added custom message if no users are online, add i.e. 'no users online' as fourth parameter


[gelöscht durch Administrator]
« Last Edit: August 30, 2010, 07:47:35 PM by MoSaG »

Offline crnogorac081

  • Posts: 1914
  • Gender: Male
Re: Snippet: Display members online
« Reply #1 on: August 27, 2010, 07:06:48 PM »
hi,

welcome to wb community :) the snippet sounds great, i am looking forward to test it

cheers
no bb in signature

Offline crnogorac081

  • Posts: 1914
  • Gender: Male
Re: Snippet: Display members online
« Reply #2 on: August 27, 2010, 07:45:55 PM »
Hi,

I did a quick review of your code..

What happens if I am online, but idle for 6 min ? I want be displayed..right :)
no bb in signature

Offline MoSaG

  • Posts: 15
Re: Snippet: Display members online
« Reply #3 on: August 27, 2010, 09:27:49 PM »
5 Minutes is the default timeout, if you are idle 6 mins you will be kicked off the list. The next time you surf on a page (and still be logged in to the system), you're status will be updated to online again. I think, that's ok, no realtime updates at the moment ...
Do you think an option "show me always on, as long as I am not logged off" makes sense? (this could be one of the possible additions, a user may change the online status, the timeout, or "I want to be invisible" ;) )
On line 21 in inlcude.php you can set a higher timeout limit. Perhaps I learn in a few days how I get an admintool to run, where you can enter the timeout. Meanwhile you have to change it by hand in the snippet. Thanks for testing!
« Last Edit: August 27, 2010, 09:38:59 PM by MoSaG »

Offline crnogorac081

  • Posts: 1914
  • Gender: Male
Re: Snippet: Display members online
« Reply #4 on: August 27, 2010, 10:24:14 PM »
For example if you are hanging on forum, and you dont surf, you are still displayed as online.. :)

It is not my intention to increase/decrease timeout , just to point that even when you are loged in & not surfing, you are still online, but idle :)

Perhaps more precise would be: "Active users in last 5 min: <list>"

And, regarding timeout, since this is a function, you can easily increase the timeout:
Code: [Select]
function MembersOnline ($custom_timeout) {

global $database;
$timestamp = time();
if ($custom_timeout = null) { $my_timeout = "300"; } else { $my_timeout = $custom_timeout;}
$timeout = $timestamp - $custom_timeout;
....
the rest of code stays as is
}
example:
<?php MembersOnline(600); ?> => timeout 10 min
<?php MembersOnline(); ?>       => timeout 5 min


cheers

Edit: by the way I am not a pro-coder myself, and I am still learning..   so dont be afraid and keep your head up :) :)
« Last Edit: August 27, 2010, 10:26:38 PM by crnogorac081 »
no bb in signature

Offline MoSaG

  • Posts: 15
Re: Snippet: Display members online
« Reply #5 on: August 28, 2010, 10:32:22 AM »
Perhaps more precise would be: "Active users in last 5 min: <list>"
And, regarding timeout, since this is a function, you can easily increase the timeout:

added support for a custom timeout and a custom headline. Perhaps I update it later to a multilanguage version ...

Offline iradj

  • Posts: 353
  • Gender: Male
Re: Snippet: Display members online
« Reply #6 on: August 30, 2010, 12:07:22 PM »
Hi,
I want to show display_name and not user name. What should I do?

BR Iradj

Offline MoSaG

  • Posts: 15
Re: Snippet: Display members online
« Reply #7 on: August 30, 2010, 01:20:19 PM »
I want to show display_name and not user name. What should I do?

Hi,
if you want to show display_name instead of username change line 56 in include.php from
Code: [Select]
   $username = $database->get_one("SELECT username FROM ".TABLE_PREFIX."users WHERE user_id = '".$row['user_id']."'");to
Code: [Select]
   $username = $database->get_one("SELECT display_name FROM ".TABLE_PREFIX."users WHERE user_id = '".$row['user_id']."'");
Have not tested, but should work ... tested, it works and thanks for this, because it makes more sense to display the display_name than the username! I'll change this on the snippet soon.
« Last Edit: August 30, 2010, 01:23:13 PM by MoSaG »

Offline iradj

  • Posts: 353
  • Gender: Male
Re: Snippet: Display members online
« Reply #8 on: August 30, 2010, 01:39:40 PM »
Thanks, it works. Can you write me why I always get one sized line.

[gelöscht durch Administrator]

Offline Stefek

  • Posts: 6177
  • Gender: Male
  • ("ړ)
Re: Snippet: Display members online
« Reply #9 on: August 30, 2010, 01:43:42 PM »
Hello,

this is a very nice snippet.

I suggest to use another parameter to have a choice between username || display_name, like:

MembersOnline ($seconds = 300, $headline = 'Active users in last 5 min', $show='username');
Makes more sense to me than a hardcoded solution.

BTW, there is another Snippet from User Ruud E. which creates a list of visitors who are not logged in.
Maybe it was a good idea to merge both, or extend this one to have a list of logged in users and a Number print of those not logged in?

Regards,
Stefek
"Gemeinsam schafft man mehr."

gemeinsam
1. mehreren Personen oder Dingen in gleicher Weise gehörend, eigen
2. in Gemeinschaft [unternommen, zu bewältigen]; zusammen, miteinander
#Duden

Offline MoSaG

  • Posts: 15
Re: Snippet: Display members online
« Reply #10 on: August 30, 2010, 03:19:58 PM »
Thanks, it works. Can you write me why I always get one sized line.
Sorry don't understand, do you mean the blank line (only the bullet) under your name?
Perhaps you give me the link to your website so I can view it in action?

Offline MoSaG

  • Posts: 15
Re: Snippet: Display members online
« Reply #11 on: August 30, 2010, 03:44:47 PM »
this is a very nice snippet.

I suggest to use another parameter to have a choice between username || display_name, like:

I've added this.

BTW, there is another Snippet from User Ruud E. which creates a list of visitors who are not logged in.

Yes I know, my snippet based on Ruuds snippet - after I understand how Ruuds snippet worked, I have to remove many lines and code nearly from scratch, because his way is completly different.

Maybe it was a good idea to merge both, or extend this one to have a list of logged in users and a Number print of those not logged in?

I don't know if I can just (legaly) copy Ruuds code to my snippet ... but you are right, it is a good idea to show not logged in users too ... I'll think about it, how to solve this.

Offline iradj

  • Posts: 353
  • Gender: Male
Re: Snippet: Display members online
« Reply #12 on: August 30, 2010, 03:51:19 PM »
Thanks, it works. Can you write me why I always get one sized line.
Sorry don't understand, do you mean the blank line (only the bullet) under your name?
Perhaps you give me the link to your website so I can view it in action?

Yes exactly what I mean.  URL is www.evgberg.de

Offline MoSaG

  • Posts: 15
Re: Snippet: Display members online
« Reply #13 on: August 30, 2010, 04:23:59 PM »
Yes exactly what I mean.  URL is www.evgberg.de

can't reproduce it, but perhaps this helps to you:

at first try to clear caches and relogg, if this does'nt help, empty database table '..._mod_members_onl ine', if this also has no effect try to change line 58 from:
Code: [Select]
$tpl->set_var(array("USERNAME" => $username));to:
Code: [Select]
if ($username) { $tpl->set_var(array("USERNAME" => $username)); }
Hope some/one of this tips will help.

EDIT: perhaps the new 0.1.4 version will solve the problem, the snippet has stored users that are not logged in with id 0 so they are always on - this will be checked now and only id higher than 0 will be stored to the members_online table, in addition there is now a custom message available if no user is online, see the updated version in 1st message.
« Last Edit: August 30, 2010, 05:13:04 PM by MoSaG »

Offline iradj

  • Posts: 353
  • Gender: Male
Re: Snippet: Display members online
« Reply #14 on: August 30, 2010, 05:56:25 PM »
Thank you. Everything works great.

Offline mava100

  • Posts: 38
Re: Snippet: Display members online
« Reply #15 on: August 13, 2013, 07:16:32 AM »
I have some error if no one is logged: notice error in line in include.php on line 45, 47, 49
It's minor problem but how I can eliminate from frontend page? I'm newbie sorry!
thanks in advance


Offline MoSaG

  • Posts: 15
Re: Snippet: Display members online
« Reply #16 on: August 13, 2013, 09:24:53 AM »
Could you please post the original error that is displayed?
And I also need the version of your WebsiteBaker installation too.

Offline marmot

  • Posts: 1103
Re: Snippet: Display members online
« Reply #17 on: August 13, 2013, 10:25:56 AM »
Hi,

Could you please post the original error that is displayed?
the reported notice says that $_SESSION['USER_ID'] is not set when user is not logged in. That's why the notice only occurs when showing the snippnet to guests.
Tried to fix this and indtroduce parameter $show_guests = 1 to also show how many guests are online in the attached file. Maybe you want to test and find somthing helpful for updating you snippnet.

regards

Offline mava100

  • Posts: 38
Re: Snippet: Display members online
« Reply #18 on: August 13, 2013, 01:03:40 PM »
dear marmot you are my best helper ...  :-) all solved thanks you again!

Offline Tomno399

  • Posts: 211
Re: Snippet: Display members online
« Reply #19 on: May 17, 2015, 12:21:30 PM »
@marmot

One Question: I miss the closing "?>" in the include.php above. Is it not necessary? I do not get any errors, but if it is working right I cant't check.

Thanks Tom

Offline DarkViper

  • Forum administrator
  • *****
  • Posts: 3034
  • Gender: Female
Re: Snippet: Display members online
« Reply #20 on: May 17, 2015, 12:36:22 PM »
One Question: I miss the closing "?>" in the include.php above. Is it not necessary? I do not get any errors, but if it is working right I cant't check.

It's right to leave out the closing ?> at the end of a PHP file. See: Coding Style Guide / Common / Files or PSR-2 :: Coding Style Guide

Manuela
« Last Edit: May 17, 2015, 12:44:59 PM by DarkViper »
Der blaue Planet - er ist nicht unser Eigentum - wir haben ihn nur von unseren Nachkommen geliehen

"You have to take the men as they are… but you can not leave them like that !" :-P
Das tägliche Stoßgebet: Oh Herr, wirf Hirn vom Himmel !

Offline Tomno399

  • Posts: 211
Re: Snippet: Display members online
« Reply #21 on: July 14, 2015, 03:10:00 PM »
hmmm, my call

Code: [Select]
MembersOnline (604800, 'Login in den letzten 7 Tagen', 'display_name', '0 users online', 0);
does not really work. Is the time too long or?

By the way Marmot's download "include.zip" is demaged. Can anyone take an upload again?

thanks tom

Offline marmot

  • Posts: 1103
Re: Snippet: Display members online
« Reply #22 on: July 14, 2015, 11:29:51 PM »
Hi,

Code: [Select]
MembersOnline (604800, 'Login in den letzten 7 Tagen', 'display_name', '0 users online', 0); does not really work. Is the time too long or?
works fine for me with wb282sp3. Of course the last parameter makes only sense with the include patch.
Quote
By the way Marmot's download "include.zip" is demaged. Can anyone take an upload again?
find the file attached.

Offline Tomno399

  • Posts: 211
Re: Snippet: Display members online
« Reply #23 on: July 15, 2015, 08:56:27 AM »
thank you marmot!

I use wb283sp4, php 5.5

I have a question of understanding about the snippet: After a login I will be shown as the first member, after a second login with another username and another password, I will be shown as second member. I think it will be the same with a third and a fourth user login (not tested). So far so good the module works!

But why I cannot see a member who was logged in two days before when I ask the last seven days? Is this memory not asked or lost in the database?

Hmm I would like to get more and more understanding ...

regards tom

Offline DarkViper

  • Forum administrator
  • *****
  • Posts: 3034
  • Gender: Female
Re: Snippet: Display members online
« Reply #24 on: July 15, 2015, 01:08:22 PM »
But why I cannot see a member who was logged in two days before when I ask the last seven days? Is this memory not asked or lost in the database?

The reason is the clean-up of the database.
i.e.: if you call the function with $seconds = 300 so all records older then 300 seconds will be deleted with that statement: $database->query("DELETE FROM $table WHERE timestamp < ".$timeout);
If you set $seconds = (60*60*24*7) // =7 days.. so you can see the records from (now - 300) to the future only. The time range grows up then till after 7 days you can see records from the whole last week.

Manuela
Der blaue Planet - er ist nicht unser Eigentum - wir haben ihn nur von unseren Nachkommen geliehen

"You have to take the men as they are… but you can not leave them like that !" :-P
Das tägliche Stoßgebet: Oh Herr, wirf Hirn vom Himmel !