WebsiteBaker Community Forum

WebsiteBaker Support (2.8.x) => Droplets & Snippets => Topic started by: MoSaG on August 27, 2010, 06:51:03 PM

Title: Snippet: Display members online
Post by: MoSaG 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:

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]
Title: Re: Snippet: Display members online
Post by: crnogorac081 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
Title: Re: Snippet: Display members online
Post by: crnogorac081 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 :)
Title: Re: Snippet: Display members online
Post by: MoSaG 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!
Title: Re: Snippet: Display members online
Post by: crnogorac081 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 :) :)
Title: Re: Snippet: Display members online
Post by: MoSaG 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 ...
Title: Re: Snippet: Display members online
Post by: iradj 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
Title: Re: Snippet: Display members online
Post by: MoSaG 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.
Title: Re: Snippet: Display members online
Post by: iradj 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]
Title: Re: Snippet: Display members online
Post by: Stefek 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
Title: Re: Snippet: Display members online
Post by: MoSaG 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?
Title: Re: Snippet: Display members online
Post by: MoSaG 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.
Title: Re: Snippet: Display members online
Post by: iradj 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
Title: Re: Snippet: Display members online
Post by: MoSaG 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.
Title: Re: Snippet: Display members online
Post by: iradj on August 30, 2010, 05:56:25 PM
Thank you. Everything works great.
Title: Re: Snippet: Display members online
Post by: mava100 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

Title: Re: Snippet: Display members online
Post by: MoSaG 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.
Title: Re: Snippet: Display members online
Post by: marmot 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
Title: Re: Snippet: Display members online
Post by: mava100 on August 13, 2013, 01:03:40 PM
dear marmot you are my best helper ...  :-) all solved thanks you again!
Title: Re: Snippet: Display members online
Post by: Tomno399 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
Title: Re: Snippet: Display members online
Post by: DarkViper 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 (http://wiki.WebsiteBaker.org/doku.php/en/dev/all/psr-2de#files) or PSR-2 :: Coding Style Guide (http://www.php-fig.org/psr/psr-2/#2.2-files)

Manuela
Title: Re: Snippet: Display members online
Post by: Tomno399 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
Title: Re: Snippet: Display members online
Post by: marmot 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.
Title: Re: Snippet: Display members online
Post by: Tomno399 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
Title: Re: Snippet: Display members online
Post by: DarkViper 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
Title: Re: Snippet: Display members online
Post by: Tomno399 on July 15, 2015, 02:22:49 PM
Quote
older then xxx seconds will be deleted ... time range grows up then

Thank you Manuela, I got it and I understand now why there is shown a difference to the user statistic modul.

I always learn something new to but unfortunately I will never be a php programmer.

Tom