idova has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks, I have been asked to install a 'who is online' feature onto our wibstie at work, now I have a good idea on how it could be done but I was wondering how others do it. What I am trying to see if anyone can know if someone is online or as moved on off-site somewhere, how do the monks here do it with the side bar, any and all suggestions would be appreciated

Replies are listed 'Best First'.
Re: how do to a 'who is online' feature
by arthas (Hermit) on May 21, 2003 at 09:56 UTC
    What you may do is to store into a database the clients who have hit your web site in the past few minutes (usually, five minutes).

    If you have authenticated users, you can use cookies (or any other authentication method you use) to identify the user every time it comes to your web site.

    If you need to count users who are "guests" you can do that by counting the different IP addresses that had hit your site in the past few minutes: it's not perfect, but it'll give you a more or less approximately correct number. The IP address of the client is in $ENV{'REMOTE_ADDR'}.

    Michele.
Re: how do to a 'who is online' feature
by dws (Chancellor) on May 21, 2003 at 10:06 UTC
    any and all suggestions would be appreciated

    arthas's answer is essentially correct. If "database" intimidates you, think "cache". A flat file works, though you'll be rewriting it each time you record a page hit from a user. Depending on your site load, this may or may not be an issue.

    No matter how you decide to capture data, it suffices to record a username and a "last visited" timestamp. The timestamp is used to determine who to show as being online. You might, for example, say that any user who has hit the website within the last 5 minutes is "online". Depending on how you're keeping records, the timestamp can also be used to expire a user from the cache.

      indeed, and if you think "cache" you should check out Cache::Cache.

      my $who_here = Cache::FileCache->new( default_expires_in => 300, # 5 minutes ); # reset current user expiration time $who_here->set($user, 1) if have_valid_user(); # if you have session data you can store it instead of # the '1' with the side benefit of logging out users # after inactivity # # my $userinfo; # unless ($userinfo = $who_here->get($user)) { # ... make them login again # $who_here->set($user, { src_ip => 'foo', favorite_colour => 'green' + }); # } else { # $userinfo has session info # print "your favorite colour is $userinfo->{favorite_colour}<br>"; # } my @who_here_now = $who_here->get_keys(); print "there are ", scalar @who_here_now, " recent visitors:<br>"; print "$_<br>" for @who_here_now;
Re: how do to a 'who is online' feature
by benn (Vicar) on May 21, 2003 at 10:26 UTC
    or has moved on off-site somewhere

    Just to add to Michele's comprehensive answer, you realise of course that the *only* thing you can tell is whether somebody's made a request to your wibstie recently...they could request one of your pages, "move off-site" by requesting another page, then request another of yours, all within your 'visitor' time and you wouldn't know about it.

    This concept of being "on" or "off" a wibstie is a confusing one, perpetuated by marketing departments and stats-sellers. Think about your own browsing habits - how often do you 'visit' a whole site, look around for a while, then 'move off' to another site? I generally have 4/5 windows open - a Newest Nodes on 20 minute refresh, a google sitting waiting etc., and at least 70% of the time I leap straight from a search into a single page, absorb /reject, then go back to the search, check out the next page etc...

    I'm not saying at all you shouldn't do it - I like seeing 'Other Users' - but you should be aware of the fact that it doesn't mean as much as your PHB's think it does. :)

    Cheers
    Ben

Re: how do to a 'who is online' feature
by Abigail-II (Bishop) on May 21, 2003 at 12:10 UTC
    While you could do such a thing for a service that requires a permanent connection, like FTP, IRC or a MUD, trying to do so for a website just shows not understanding the fundamentals of the service.

    HTTP is a simple protocol which basically goes: make connection, do request, get response, break connection, although with HTTP/1.1, if you have a batch of requests, you don't have to create a new connection for all of them. But one doesn't stay connected, so a "who is online" feature is just as meaningful as the current temperature of the site.

    Abigail

Re: how do to a 'who is online' feature
by newrisedesigns (Curate) on May 21, 2003 at 23:11 UTC
Re: how do to a 'who is online' feature
by idova (Novice) on May 22, 2003 at 10:16 UTC
    thanks to all that have replied. The answer was as I thought, I didn't think there was a way to force the http system to do anything more complicated but I know I dont know it all and the number of times I made something work when others said it couldn't be done then I hoped there might of been a bit of understanding that I didn't posses. Again Thank you to all, I know what must be done now.