Bryan

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  37 Posts :: 3 Stories :: 24 Comments :: 0 Trackbacks
There is one perl cgi system which runs on apache server,It's open to everyone and no users stored in file systems or Databases, client wants to know how many users are using this system. How?

The only way I know is to use CGI::Session and cookie. each time,one client uses the system, we are using the CGI::Session to create one session , and this session should not duplicate for one user who are using the same browser(IE eg), and of course may differ for different browsers. The session will create session files in specified temp folder. each new session ,one session file. Then we can use one pl script to list active users ,actually ,that should be the number of session files in the session directory. And one thing we need to do is that we need to delete the expired session files before we want to compute the number of active users. we can use the module CGI::Session::ExpireSessions and delete the session files according to the session time-out age.

$D = {'_SESSION_ID' => '772278a8aa05506449b4e72582d09f95','_SESSION_ATIME' => 1366012456,'_SESSION_REMOTE_ADDR' => '135.251.33.31','_SESSION_CTIME' => 1366012456};;$D

session.pl
#!C:/Perl/bin/perl

use strict;
use CGI;
use warnings;
use CGI::Session;

#create session,if exists,use that one
my $cgi = CGI->new;
my $session = CGI::Session->new( undef, $cgi, {Directory=>'c:/tmp'} );

my $cookie = $cgi->cookie(CGISESSID => $session->id );
print $cgi->header(-cookie=>$cookie);
my $sid = $session->id();
#print $sid;
#set session expired date
#$session->expire(30);     
print "  <a href=\"count.pl?sid=$sid\">list active users</a>";

count.pl
#!C:/Perl/bin/perl

use strict;
use CGI;
use warnings;
use File::Find::Rule;
use CGI::Session;
use CGI::Session::ExpireSessions;

print "Content-type: text/html\n\n";

#clear expired session
CGI::Session::ExpireSessions -> new(delta => 30, temp_dir => 'c:\tmp', verbose => 0-> expire_file_sessions();

my @files = File::Find::Rule->file()->in('c:\tmp');
print "The number of active users is : ";
print scalar @files, $/;

I suppose the count.pl are always using the same session with the session.pl.And we need to also install the module File::Find::Rule.

Apache HTTP Server Version 2.0 
ActivePerl 5.16.3 Build 1603
ActiveState Komodo Edit 7


posted on 2013-04-15 15:58 Life is no respector of any genius. 阅读(274) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: