接了个单子,perl的题目。
已经安装好了xampp
https://www.apachefriends.org/xampp-files/7.4.5/xampp-linux-x64-7.4.5-0-installer.run
改配置文件/opt/lampp/etc/httpd.conf
开启alias_module
ScriptAlias /cgi-bin/ "/opt/lampp/cgi-bin/"
创建测试文件:
/opt/lampp/cgi-bin/download.cgi
#!/usr/bin/perl
# HTTP Header
print "Content-Type:application/octet-stream; name=\"password.txt\"\r\n";
print "Content-Disposition: attachment; filename=\"password.txt\"\r\n\n";
# Actual File Content will go hear.
open( FILE, "
如果报错500,可以查看默认log
tail -f /opt/lampp/logs/error_log
End of script output before headers: test.cgi
AH01215: (13)Permission denied: exec of '/opt/lampp/cgi-bin/test.cgi' failed: /opt/lampp/cgi-bin/test.cgi
perl文件必须有执行权限, chmod a+x test.cgi
cgi程序必须输出header,header结尾必须\r\n\r\n
访问:
http://ip地址端口号/cgi-bin/download.cgi
得到
参考教程:
https://www.runoob.com/perl/perl-arrays.html
https://www.runoob.com/perl/perl-subroutines.html
https://www.runoob.com/perl/perl-files.html
https://www.runoob.com/perl/perl-cgi-programming.html
perl接收post参数:
*/opt/lampp/htdocs/test.html
菜鸟教程(runoob.com)
访问/test.html
* /opt/lampp/cgi-bin/post.cgi
#!/usr/bin/perl
sub formInput {
my ($buffer, @pairs, $pair, $name, $value);
# Read text content
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}else {
$buffer = $ENV{'QUERY_STRING'};
}
# Read name/value pair
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
return $FORM;
}
$FORM = formInput();
$name = $FORM{name};
$url = $FORM{url};
print "Content-type:text/html\r\n\r\n";
print "";
print "";
print '';
print '菜鸟教程(runoob.com) ';
print "";
print "";
print "$name网址:$url
";
print "";
print "";
跳转/cgi-bin/post.cgi
做个题目:
* hw7_1.html
hw7_1
* hw7_1.dat
Question number Correct answer
1 "D"
2 "C"
3 "A"
4 "B"
* hw7_1.pl
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
sub file_get_contents {
my $fileName = $_[0];
open(DATA, "<".$fileName) or die "can not open file ".$fileName." $!";
$s = "";
while() {
$s .= "$_";
}
close(DATA);
return $s;
}
sub parse_answers {
my $s = $_[0];
my @lines = split(/\n/, $s);
my @a = ();
# git rid of title
shift(@lines);
@answers = ();
# 1 "D"
foreach $line (@lines) {
@a = split(/\t/, $line);
# remove ""
$ans = substr($a[1], 2, 1);
push(@answers, $ans);
}
return @answers;
}
sub formInput {
my ($buffer, @pairs, $pair, $name, $value);
# Read text content
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}else {
$buffer = $ENV{'QUERY_STRING'};
}
# Read name/value pair
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
return $FORM;
}
sub in_array {
my $needle = @_[0];
my @haystack = @_[1];
foreach $e (@haystack) {
if ($needle eq $e) {
return 1;
}
}
return 0;
}
$s = file_get_contents("hw7_1.dat");
@answers = parse_answers($s);
# post data
$FORM = formInput();
# checking logic
$QUESTION_COUNT = @answers;
local $i;
@possibleAns = ("A", "B", "C", "D");
for ($i = 0; $i < $QUESTION_COUNT; $i += 1) {
$inputAns = $FORM{'a'.$i};
if (!in_array($inputAns, $possibleAns)) {
print "Invalid answer for question ".($i+1);
die;
}
}
# show correct answer (D C A B)
# print "Correct answers are: ";
# foreach $ans (@answers) {
# print $ans;
# }
# print "
";
# show score
local $correctCount = 0;
local $scorePerQ = 100 / $QUESTION_COUNT;
$i = 0;
local $correctCount = 0;
foreach $ans (@answers) {
$input = $FORM{'a'.$i};
$i += 1;
# print $input."
";
if ($input eq $ans) {
$correctCount += 1;
} else {
print "Question#".$i." Your answer: ".$input.", correct:".$ans."
";
}
}
print "Your score: ". ($scorePerQ * $correctCount)."
";
print "Percentage of answers correct: ".(($correctCount/$QUESTION_COUNT)*100)."%";
in_array函数不起作用,不知道怎么改
下一个题目:
* hw7_2.html
hw7_2
Enter password:
* hw7_2.pl
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
sub formInput {
my ($buffer, @pairs, $pair, $name, $value);
# Read text content
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}else {
$buffer = $ENV{'QUERY_STRING'};
}
# Read name/value pair
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
return $FORM;
}
sub file_get_contents {
my $fileName = $_[0];
if (!open(DATA, "<".$fileName)) {
return "";
}
$s = "";
while() {
$s .= "$_";
}
close(DATA);
return $s;
}
sub file_append_contents {
my $fileName = $_[0];
my $content = $_[1];
open(DATA, "+>>".$fileName) or die("open ".$fileName." failed");
print DATA $content;
}
$FORM = formInput();
$pwd = $FORM{pwd};
# print $pwd;
local @valid = ("rosebud", "redbuffer", "jackson", "surrender", "dorothy");
local $e;
local $correct = 0;
for (my $i = 0; $i < @valid; $i++) {
$e = $valid[$i];
# print "needle=[".$pwd."], e=[".$e."]
";
if ($pwd eq $e) {
$correct = 1;
break;
}
}
if ($correct == 1) {
print "";
print "Home page
";
exit 0;
}
file_append_contents("hw7_2.dat", $pwd."\n");
$s = file_get_contents("hw7_2.dat");
my @lines = split(/\n/, $s);
# If the third entered password is invalid, a JavaScript alert "access denied" should be launched.
if (@lines > 2) {
print "Access denied";
exit 1;
}
print "";
简书上另外一个题目:
Common Factors Calculator