nitic cs exp perl
1.0.0
讲座于2020年开放
counter.cgi要计算出现网页的次数,您需要一个文件来存储计数数据。以下是一个基本的PERL CGI程序。该程序读取计数器文件,增加计数,并将更新的计数写回文件和网页。
src/counter.cgi # !/usr/bin/perl
use strict;
use warnings;
use CGI qw( :standard ) ;
# カウンタファイルのパスを指定
my $counter_file = ' counter.txt ' ;
# カウントを取得および更新
open my $fh , ' +< ' , $counter_file or die " Can't open $counter_file : $! " ;
my $count = < $fh >;
$count = 0 unless defined $count ;
$count ++;
seek $fh , 0, 0;
print $fh $count ;
close $fh ;
# HTMLページを出力
print header;
print start_html( ' Counter Page ' );
print " This page has been viewed $count times. " ;
print end_html;要运行此CGI程序,请执行以下步骤:
保存程序:
counter.cgi的文件中。设置执行权限:
chmod +x src/counter.cgi配置XAMPP :
配置CGI目录:
/Applications/XAMPP/etc/httpd.conf ),并设置CGI目录如下: <Directory "/Applications/XAMPP/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>
计划安置:
ファイルを复制到/applications/xampp/cgi-bin`目录。/Applications/XAMPP/cgi-bin目录中创建符号链接counter.cgi ,XAMPP apache服务器即使实际counter.cgi文件位于其他地方,XAMPP Apache服务器也可以正确识别并运行。要创建一个符号链接,请打开终端并运行以下命令(替换实际counter.cgi文件的路径和链接适当的目的地): $ ln -s src/counter.cgi /Applications/XAMPP/cgi-bin/counter.cgi此命令在/Applications/XAMPP/cgi-bin目录中创建一个名为counter.cgi的符号链接,该目录指向实际的counter.cgi文件。然后,您可以使用Web浏览器访问http://localhost/cgi-bin/counter.cgi来运行CGI程序。
http://localhost/cgi-bin/counter.cgi 。这就是计数器程序应运行的方式,并且每次访问网页时都应增加计数。计数器值也保存在counter.txt文件中,并且程序从该文件读取计数值。