后一页
前一页
回目录

6.2 计数器


下面是一个简单的计数器程序:

#******** BEGIN BODY ********
open (GETCOUNT,"<count.log");            #open count log for input
$counter=<GETCOUNT>;                      #assign contents of file to $counter
close (GETCOUNT);                              #close access to file

$counter++;                                                 #increase $counter by 1.
print "$counter hits";                                #print number of hits to users screen

open (PUTCOUNT,">count.log");            #open count log for output
print PUTCOUNT ($counter);                #replace old counter value with new one
close PUTCOUNT;                                # close access to file

#******** END BODY *********

如果用SUBMIT按钮,当前页将消失,出现程序的输出页。为了在当前页上显示点击数,需要有一个页面转载时执行的程序,然后把结果潜入相同页面上。这种方法叫做Server Side Include(SSI)。

注意:不是所有的web服务器都支持SSI。有些只在页面文件以".shtml"或".html-ssi"结束才起作用,有些由于安全原因完全屏蔽了这种功能。

下面是代码的工作过程:


如果不起作用,直接浏览count.cgi看它是否工作。一旦CGI起作用,把页面文件重命名为.shtml扩展名。


后一页
前一页
回目录