Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
more useful options
 
PerlMonks  

Listing Active CGI::Sessions

by Anonymous Monk
on Sep 05, 2004 at 15:21 UTC ( #388617=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I can't seem to find anywhere on the net that will explain how can i list all active CGI::Sessions?

I'm building a site where i want to list the total number of active users that are on.
I'm sure there is a way to do it. I'm also using MySQL to store the data.

Any Help would be greatly appreciated.

Comment on Listing Active CGI::Sessions
Re: Listing Active CGI::Sessions
by PodMaster (Abbot) on Sep 05, 2004 at 15:34 UTC
    What is an active CGI::Session? Once you can explain that you can begin to devise a way to go about listing active sessions.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      I'm looking for all sessions that have not expired yet.
Re: Listing Active CGI::Sessions
by davido (Archbishop) on Sep 05, 2004 at 15:35 UTC

    Just keep track of session ID's. You can do this by storing session ID and its accompanying session info in your DB. Any time a session is running, just re-check to see if any of the times in the DB have expired. Then it's just a matter of counting up how many haven't expired yet.

    For session key and expiration information, have a look at the POD for CGI::Session.


    Dave

      I have mysql tracking all the session info. what i'm trying to accomplish is having administrators and users see how many current live(unexpired) sessions there are (i.e. Current Member Online: 334). I've checked the Perl Docs for CGI::Session and it doesn't seem to explain how to go about doing this.

      The administrator wants this feature in order to cancel/delete sessions of abusive members.

      Thanks for the reply.
        If you're looking for something like a count of total memebers signed in or a list of all memebers signed in then you could do what we did.

        That is just have each page request store which user the page was requested for and the current time in a database table. Make the UserID field unique and it shouldn't grow out of hand. When looking to get stats you can just SELECT COUNT(*) FROM ActiveSessions or SELECT UserID FROM ActiveSessions. I would suggest clearing old records from the table with a 5 minute cron or a pseudo-random system (maybe 1 in 500 page loads).

        Another thing you can do is actually have summary data that is generated either a) every say 5 minutes or b) whenever that data is changed (ie someone logged in or out). This plan works better on things that don't change very often or where you have a high number of records to deal with.

        Note that this method can get database intensive when you get a lot of site traffic. For the above linked site the sessions database does not run on the same machines that run the site's actual content database.

        PS: Yes I wrote machines as in plural. 60 Gigs of of web files (ie not including protocol overhead) is not an odd weekday. I think those databases get a fair bit of work for a website. - frink
Re: Listing Active CGI::Sessions
by antirice (Priest) on Sep 05, 2004 at 18:44 UTC

    If you're using CGI::Session::MySQL, you can alter it to put the expiration time in a column.

    sub store { my ($self, $sid, $options, $data) = @_; my $dbh = $self->MySQL_dbh($options); my $lck_status = $dbh->selectrow_array(qq|SELECT GET_LOCK("$sid", +10)|); unless ( $lck_status == 1 ) { $self->error("Couldn't acquire lock on id '$sid'. Lock status: + $lck_status"); return undef; } $dbh->do(qq|REPLACE INTO $TABLE_NAME (id, expires, a_session) VALU +ES(?,FROM_UNIXTIME(?),?)|, undef, $sid, $self->expire(), $self->freeze($data)); return $dbh->selectrow_array(qq|SELECT RELEASE_LOCK("$sid")|); }

    And the schema just needs to be updated:

    CREATE TABLE sessions ( id CHAR(32) NOT NULL UNIQUE, expires datetime, a_session TEXT NOT NULL );

    Now in order to find all active sessions, run the query:

    select id from sessions where expires <= NOW() or expires is null

    ...or something like that ;-P

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

Re: Listing Active CGI::Sessions
by shenme (Priest) on Sep 05, 2004 at 19:09 UTC
    You might want to look over the code in CGI-Session-ExpireSessions for an example of how to process/examine the existing sessions entries.
Re: Listing Active CGI::Sessions
by CountZero (Chancellor) on Sep 05, 2004 at 20:00 UTC
    All will depend on your definition of expired sessions.

    If you set the expiration period to be a few days, you will have many members "online"!

    Which shows once again, that the concept of "session" is rather artificial in the context of http.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://388617]
Approved by sunadmn
Front-paged by Elijah
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (17)
As of 2014-04-18 14:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (469 votes), past polls