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文件中,並且程序從該文件讀取計數值。