apache perl 环境配置

windows环境安装Perl

https://platform.activestate.com/ActiveState/Perl-5.32/distributions?platformID=78977bc8-0f32-519d-80f3-9043f059398c

powershell

powershell -Command "& $([scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://platform.activestate.com/dl/cli/pdli01/install.ps1'))) -activate-default ActiveState/Perl-5.32 "

 

已经安装好了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

<IfModule alias_module>                                                                                                                             
    ScriptAlias /cgi-bin/ "/opt/lampp/cgi-bin/"
</IfModule>

创建测试文件:

/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, "</etc/passwd" );
while(read(FILE, $buffer, 100) )
{
   print("$buffer");
}

如果报错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

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
  </head>
  <body>
    <form action="/cgi-bin/post.cgi" method="post">
      站点名称: <input type="text" name="name">  <br />

      站点 URL: <input type="text" name="url" />
      <input type="submit" value="提交" />
    </form>
  </body>
</html>

访问/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 "<html>";
print "<head>";
print '<meta charset="utf-8">';
print '<title>菜鸟教程(runoob.com)</title>';
print "</head>";
print "<body>";
print "<h2>$name网址:$url</h2>";
print "</body>";
print "</html>";

跳转/cgi-bin/post.cgi

做个题目:

* hw7_1.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hw7_1</title>
    <style>
      .question {margin-bottom: 10px;}
    </style>
  </head>
  <body>
    <form action="hw7_1.pl" method="POST">
      <div class="question">
	<div class="text">1. Agriculture is the country’s chief source of wealth, wheat ____ by far the biggest cereal crop.</div>
	<input type="radio" name="a0" value="A" />A.is
	<input type="radio" name="a0" value="B" />B.been
	<input type="radio" name="a0" value="C" />C.be
	<input type="radio" name="a0" value="D" />D.being
      </div>

      <div class="question">
	<div class="text">2. Jack ____from home for two days now, and I am beginning to worry about his safety.</div>
	<input type="radio" name="a1" value="A" />A. had been missing
	<input type="radio" name="a1" value="B" />B. has been missed
	<input type="radio" name="a1" value="C" />C. has been missing
	<input type="radio" name="a1" value="D" />D. was missing
      </div>

     <div class="question">
       <div class="text">3. Above the trees are the hills, ____ magnificence the river faithfully reflects on the surface.</div>
       	<input type="radio" name="a2" value="A" />A. whose
	<input type="radio" name="a2"  value="B" />B. of whose
	<input type="radio" name="a2" value="C" />C. where
	<input type="radio" name="a2" value="D" />D. which
      </div>

      <div class="question">
	<div class="text">4. Who____ was coming to see me in my office this afternoon?</div>
	<input type="radio" name="a3" value="A" />A. you said
	<input type="radio" name="a3" value="B" />B. did you say
	<input type="radio" name="a3" value="C" />C. did you say that
	<input type="radio" name="a3" value="D" />D. you did say
      </div>
      
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>

* 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(<DATA>) {
	$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 "<br />";

# 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."<br />";
    if ($input eq $ans) {
	$correctCount += 1;
    } else {
	print "Question#".$i." Your answer: ".$input.", correct:".$ans."<br />";
    }
}

print "Your score: ". ($scorePerQ * $correctCount)."<br />";
print "Percentage of answers correct: ".(($correctCount/$QUESTION_COUNT)*100)."%";

in_array函数不起作用,不知道怎么改

下一个题目:

* hw7_2.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hw7_2</title>
    <style>
    </style>
  </head>
  <body>
    <p>Enter password:</p>
    <form action="hw7_2.pl" method="GET">
      <input type="password" name="pwd" value="" />
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>

* 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(<DATA>) {
	$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."]<br />";
    if ($pwd eq $e) {
	$correct = 1;
	break;
    }
}

if ($correct == 1) {
    print "<script>alert(\"Access granted\")</script>";
    print "<p>Home page</p>";
    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 "<script>alert(\"Try again\"); window.location.href=\"hw7_2.html\"</script>";

简书上另外一个题目:

Common Factors Calculator

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fareast_mzh

打赏个金币

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值