第三章 计数器的编写方法(未定稿)
一、记录(log)文件01: dialup-9.austin.io.com - - [02/Oct/1995:20:18:05 -0500] "GET /phoenix/ HTTP/1.0" 200 2330注;当主页在srm.conf中被命名为welcome.html、index.cgi、index.shtml等时,对其的访问记录,可能只含有目录名而不包含该文件名。
02: crossnet.org - - [08/Oct/1995:19:56:45 -0500] "HEAD / HTTP/1.0" 200 0
03: dialup-2.austin.io.com - - [09/Oct/1995:07:54:56 -0500] "GET /leading-rein/orders HTTP/1.0" 401 -
04: onramp1-9.onr.com - - [10/Oct/1995:11:11:40 -0500] "GET / HTTP/1.0" 200 1529
05: onramp1-9.onr.com - - [10/Oct/1995:11:11:43 -0500] "GET /accn.jpg HTTP/1.0" 200 20342
06: onramp1-9.onr.com - - [10/Oct/1995:11:11:46 -0500] "GET /home.gif HTTP/1.0" 200 1331
07: dialup-3.austin.io.com - - [12/Oct/1995:08:04:27 -0500] "GET /cgi-bin/env.cgi?
08: SavedName=+&First+Name=Eric&Last+Name=Herrmann&Street=&City=&State=&
09: zip=&Phone+Number=%28999%29+999-9999+&Email+Address=&
10: simple=+Submit+Registration+ HTTP/1.0" 200 1261
11: dialup-20.austin.io.com - - [14/Oct/1995:16:40:04 -0500] "GET /leading-rein/index.cgi?unique_id=9658-199.170.89.58-813706781 HTTP/1.0" 200 1109
1: #!/usr/local/bin/perl现在就可以在主页中加上SSI指令来显示计数了,例如:
2: print "content-type: text/html\n\n";
3: $num = `grep -c 'GET / HTTP' /your-server-root/logs/access_log` ;
4: $num += `grep -c 'GET /index.shtml' /your-server-root/logs/access_log` ;
5: $num += `grep -c 'GET /index.html' /your-server-root /logs/access_log` ;
6: print "$num\n";
01: <html>别忘了把此文件扩展名改为.shtml。在grep.cgi中,grep命令中包围模式的单引号告诉UNIX shell不改变该串的内容以精确匹配。
02: <head><title>grep test</title>
03: <body>
04: <hr noshade>
05: This page has been accessed
06: <!--#exec cgi="grep1.cgi" --> times.
07: <hr noshade>
08: </body>
09: </html>
1: dbmopen(%COUNTERS, $DOCUMENT_ROOT/DBM_FILES/counters,0666);2、文本文件
2: if(!(defined($counters{'my_counter'})){
3: $counters{'my_counter'}=0;}
4: $counters{'my_counter'})++;
5: $count=$counters{'my_counter'};
6: dbmclose (counters);
1)打开文件3、文件锁定
2)读取计数
3)自增
4)写入新值
5)关闭文件
01: While(-f counter.lock){首先检查锁定标志文件是否存在,如果存在,就说明另一个进程正在使用该文件,于是等待直到该文件(此处命名为counter.lock)不存在为止。此处用select()的特殊形式循环等待,此语句使程序进入休眠状态一段时间,该时间段由最后一个参数定义。之所以不用sleep()函数是因为其基本单位为秒,对这种文件锁定而言太长了,几个微秒就足够了。
02: select(undef,undef,undef,0.1);}
03: open(LOCKFILE,">counter.lock);
04: dbmopen(%COUNTERS, $DOCUMENT_ROOT/DBM_FILES/counters,0666);
05: if(!(defined($counters{'my_counter'})){
06: $counters{'my_counter'}=0;}
07: $counters{'my_counter'})++;
08: $count=$counters{'my_counter'};
09: dbmclose (counters);
10: close(LOCKFILE);
11: unlink(counter.lock);
1:定义共享锁。对计数器而言不适用。使用flock()实现的文件锁定例子如下:
2:定义排他锁。
3:定义非阻止锁。此处亦不用。
4:解除锁定。
1a: dbmopen(%counters,"filename", 0666);4、输出计数结果
or
1b: OPEN(counters,"<filename")'
2: flock(counters,2);
3: if(!(defined($counters{'my_counter'})){
4: $counters{'my_counter'}=0;}
5: $counters{'my_counter'})++;
6: $count=$counters{'my_counter'};
7: dbmclose (counters);
8: flock(counters,8);
1)用上面谈到的SSI方法输出。5、www Homepage Access Counter
2)创建各种文本格式输出。
3)生成各种漂亮的图形结果输出,本教程的《动态创建图像》一章讲述了基本原理并提供了一个x-bitmap格式的小例子,下面介绍两个更完善和漂亮的程序/库,这两个例子均需要C编译器。
#!/usr/bin/perl
use GD;
# create a new image
$im = new GD:Image(100,100);
# allocate some colors
$white = $im->colorAllocate(255,255,255);
$black = $im->colorAllocate(0,0,0);
$red = $im->colorAllocate(255,0,0);
$blue = $im->colorAllocate(0,0,255);
# make the background transparent and interlaced
$im->transparent($white);
$im->interlaced('true');
# Put a black frame around the picture
$im->rectangle(0,0,99,99,$black);
# Draw a blue oval
$im->arc(50,50,95,75,0,360,$blue);
# And fill it with red
$im->fill(50,50,$red);
# Convert the image to GIF and print it on standard output
print $im->gif;