Lectures open in 2020
counter.cgiTo count the number of times a web page appears, you need a file to store the count data. Below is a basic Perl CGI program. The program reads the counter file, increases the count, and writes the updated count back to the file and web page.
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;To run this CGI program, follow these steps:
Saving the program :
counter.cgi .Setting execution permissions :
chmod +x src/counter.cgiConfiguring XAMPP :
Configuring the CGI Directory :
/Applications/XAMPP/etc/httpd.conf ) in the editor and set the CGI directory as follows: <Directory "/Applications/XAMPP/cgi-bin">
AllowOverride None
Options ExecCGI
Order allow,deny
Allow from all
</Directory>
Program placement :
ファイルをto the /Applications/XAMPP/cgi-bin` directory.counter.cgi in /Applications/XAMPP/cgi-bin directory, XAMPP Apache servers can recognize and run it correctly, even if the actual counter.cgi file is located elsewhere. To create a symbolic link, open a terminal and run the following command (replace the path of the actual counter.cgi file and the destination of the link appropriately): $ ln -s src/counter.cgi /Applications/XAMPP/cgi-bin/counter.cgi This command creates a symbolic link named counter.cgi in /Applications/XAMPP/cgi-bin directory, which points to the actual counter.cgi file. You can then run the CGI program by visiting http://localhost/cgi-bin/counter.cgi using a web browser.
http://localhost/cgi-bin/counter.cgi . That's how the counter program should run and the count should increase every time you access the web page. The counter value is also saved in the counter.txt file, and the program reads the count value from that file.