CS351 – CGI & Perl
CGI Web Programming in Perl
Instructor: Scott Spetka, Ph.D.
Menu:
- Hello World
- Hello World Loop
- Read and display a file
- Read and display a file (Stack Version)
- Job Tracking Form
- Add & Average
- Add & Average (Queue Version)
- Hidden Inputs
- Hidden Inputs (2D Array Version)
- Path Info
- Path Info (2D Array Version)
- Line By Line File Viewer
- Line By Line File Viewer (Hash Version)
- Line By Line File Viewer (Cookie Version)
- Client Side Image Map
- Server Side Image Map
- GD Bar Graph
- Biggest File
- Graded Exam
Hello World:
#!/usr/bin/perl #Ronny Bull CS351 #Assignment for Week 3 #Print Hello World to the screen. #turn off buffering $| = 1; #set content type print "Content-type:text/html\n\n"; #print hello world to screen print "Hello World\n";
Hello World Loop:
#!/usr/bin/perl
#Ronny Bull CS351
#Assignment for week 3
#print Hello World to the screen 10 times using a loop.
#turn off buffering
$| = 1;
#set content type
print "Content-type:text/html\n\n";
#create the loop
for($count=1; $count<11; $count++)
#print hello world to screen
{
print "Hello World<br />";
}
Read and display a file:
#!/usr/bin/perl
#Ronny Bull CS351
#Assignment for week 3
#Read the file file.txt and print the contents to the screen.
#turn off buffering
$| = 1;
#set content type
print "Content-type:text/html\n\n";
#open the file
open(FILE, "file.txt") || die "Could not open the file: $!\n";
#print the contents of the file to the screen
while(<FILE>)
{
print $_;
}
#close the file
close(FILE);
Read and display a file (Stack Version):
#!/usr/bin/perl
#Ronny Bull CS351
#Assignment for week 3
#Read the file file.txt and print the contents to the screen.
use strict;
use warnings;
my @stack;
#turn off buffering
$| = 1;
#set content type
print "Content-type:text/html\n\n";
#open the file
open(FILE, "file.txt") || die "Could not open the file!: \n";
#read the file into the stack line by line
while(<FILE>)
{
push (@stack,$_);
}
#print the file to the screen by poping it out of the stack
#prints the file lines in reverse order
while(@stack)
{
print pop(@stack);
}
#close the file
close(FILE);
Job Tracking Form:
#!/usr/bin/perl
#Ronny L. Bull - CS351
#Week 4 project - Develope a CGI application
#to accept input values and echo them
#9-11-09
#Turn off buffering
$| = 1;
#Set content type
print "Content-Type: text/html\n\n";
#Check to see if the user submitted the form, if so the CONTENT_LENGTH
#must be greater than 0
if ($ENV{'CONTENT_LENGTH'} == 0 )
{
#dislay the form in the browser
&sendForm;
exit;
}
#Get the form input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Put the buffer into an array and split the values with an &
@pairs = split(/&/, $buffer);
#Fix any characters thay may need replacing to display properly
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9])([a-fA-F0-9])/pack("C", hex($1.$2))/eg;
if ( $contents{$name} eq "") {
$contents{$name} = $value;
}
else {
$contents{$name} .= " " . $value ;
}
}
#Display the input values on the screen
print "<h3>Thank you for submission!</h3> <br />";
print "You have entered the following information: <br /> <br />";
print "<b>Tech Name:</b> $contents{'name'} <br /> ";
print "<b>Customer:</b> $contents{'customer'} <br /> ";
print "<b>Job:</b> $contents{'job'} <br /> ";
print "<b>Location:</b> $contents{'location'} <br /> ";
print "<b>Date:</b> $contents{'month'}, $contents{'day'}, $contents{'year'} <br /> ";
print "<b>Start Time:</b> $contents{'start'} <br /> ";
print "<b>End Time:</b> $contents{'end'} <br /> ";
print "<b>Mileage:</b> $contents{'mileage'} <br /> ";
print "<b>Total Expenses:</b> $contents{'expenses'} <br /> ";
print "<b>Job Description:</b> $contents{'description'} <br /> ";
print "<b>Work Performed:</b> $contents{'work'} <br /> ";
print "<b>Work Completed:</b> $contents{'complete'} <br /> ";
print "<b>Materials Used:</b> $contents{'materials'} <br /> ";
print "<b>Cat 5e Cable Length:</b> $contents{'cablesize'} <br /> <br />";
#Check if the default submit button was used
if ($contents{'submit'} eq "Submit" )
{
print "<b>Your no fun you used the default submit button!</b> <br /> ";
}
#Check if Submit With IP was used if so display the users IP Address
if ($contents{'multisub'} eq "Submit With IP" )
{
print "<b>Your IP Address is:</b> $ENV{'REMOTE_ADDR'} <br /> ";
}
#Check if Submit and Return Script Name was used if so display the users DNS Hostname
if ($contents{'multisub'} eq "Submit and Return Script Name" )
{
print "<b>This script is called:</b> $ENV{'SCRIPT_NAME'} <br /> ";
}
#sendForm subsection
sub sendForm
{
#The form html code
print <<JOBFORM;
<html>
<head>
<title>Ronny L. Bull - Week 4 Project</title>
</head>
<body>
<h3>Job Tracking Form:</h3>
<form method="POST" action="week4.cgi">
<b>Tech Name:</b><br />
<input type="text" name="name" value="TechName"><br /><br />
<b>Customer Name:</b><br />
<input type="text" name="customer" value="CustName"><br /><br />
<b>Job Name:</b><br />
<input type="text" name="job" value="JobName"><br /><br />
<b>Work Location:</b>
<input type="radio" name="location" value="Remote" /> Remote
<input type="radio" name="location" value="Onsite" checked /> Onsite<br /><br />
<b>Date:</b>
<select name="month">
<option value="Jan">Jan</option>
<option value="Feb">Feb</option>
<option value="Mar">Mar</option>
<option value="Apr">Apr</option>
<option value="May">May</option>
<option value="Jun">Jun</option>
<option value="Jul">Jul</option>
<option value="Aug">Aug</option>
<option selected value="Sept">Sept</option>
<option value="Oct">Oct</option>
<option value="Nov">Nov</option>
<option value="Dec">Dec</option>
</select>
<select name="day">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option selected value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="year">
<option selected value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select><br /><br />
<b>Start Time:</b>  
<select name="start">
<option selected value="0:00">0:00</option>
<option value="1:00">1:00</option>
<option value="2:00">2:00</option>
<option value="3:00">3:00</option>
<option value="4:00">4:00</option>
<option value="5:00">5:00</option>
<option value="6:00">6:00</option>
<option value="7:00">7:00</option>
<option value="8:00">8:00</option>
<option value="9:00">9:00</option>
<option value="10:00">10:00</option>
<option value="11:00">11:00</option>
<option value="12:00">12:00</option>
<option value="13:00">13:00</option>
<option value="14:00">14:00</option>
<option value="15:00">15:00</option>
<option value="16:00">16:00</option>
<option value="17:00">17:00</option>
<option value="18:00">18:00</option>
<option value="19:00">19:00</option>
<option value="20:00">20:00</option>
<option value="21:00">21:00</option>
<option value="22:00">22:00</option>
<option value="23:00">23:00</option>
<option value="24:00">24:00</option>
</select>
<b>End Time:</b>  
<select name="end">
<option value="0:00">0:00</option>
<option value="1:00">1:00</option>
<option value="2:00">2:00</option>
<option value="3:00">3:00</option>
<option value="4:00">4:00</option>
<option value="5:00">5:00</option>
<option value="6:00">6:00</option>
<option value="7:00">7:00</option>
<option value="8:00">8:00</option>
<option value="9:00">9:00</option>
<option value="10:00">10:00</option>
<option value="11:00">11:00</option>
<option value="12:00">12:00</option>
<option value="13:00">13:00</option>
<option value="14:00">14:00</option>
<option value="15:00">15:00</option>
<option value="16:00">16:00</option>
<option value="17:00">17:00</option>
<option value="18:00">18:00</option>
<option value="19:00">19:00</option>
<option value="20:00">20:00</option>
<option value="21:00">21:00</option>
<option value="22:00">22:00</option>
<option value="23:00">23:00</option>
<option selected value="24:00">24:00</option>
</select><br /><br />
<b>Mileage:</b> <br />
<input type="text" name="mileage" value="123"><br /><br />
<b>Total Expenses:</b> <br />
<input type="text" name="expenses" value="34.56"><br /><br />
<b>Job Description:</b><br />
<textarea name="description" rows="7" cols="60">Insert Job Description Here!</textarea><br /><br />
<b>Work Performed:</b><br />
<textarea name="work" rows="7" cols="60">Describe the Work Performed Here!</textarea><br /><br />
<b>Work Completed:</b>
<input type="checkbox" name="complete" value="completed" checked /><br /><br />
<b>Materials Used:</b><br /><br />
<input type="checkbox" name="materials" value="Cable" checked /> Cat5e Cable    
<input type="checkbox" name="materials" value="Ends" checked /> Cat5e Ends<br />
<input type="checkbox" name="materials" value="Fasteners" /> Wire Fasteners
<input type="checkbox" name="materials" value="Ties" /> Wire Ties<br /><br />
<b>Cat5e Cable Length:</b><br />
<i>If more then one cable was used, select multiple lengths (if needed)</i><br /><br />
<select name="cablesize" multiple size=4>
<option selected value="10">10FT</option>
<option value="20">20FT</option>
<option selected value="30">30FT</option>
<option selected value="40">40FT</option>
<option value="50">50FT</option>
<option value="60">60FT</option>
<option value="70">70FT</option>
<option value="80">80FT</option>
<option value="90">90FT</option>
<option value="100">100FT</option>
</select><br /><br /><br />
<input type="submit" name="submit" value="Submit">
<input type="submit" name="multisub" value="Submit With IP">
<input type="submit" name="multisub" value="Submit and Return Script Name">
<input type="reset" value="Defaults">
</form>
</body>
</html>
JOBFORM
}
Add & Average:
#!/usr/bin/perl
#Ronny L. Bull - CS351
#Week 5 project - Develope a CGI application that:
#Requests a number of elements to add and average on the first page
#Outputs blanks for each element to be filled in on the second page
#Prints the inputs as well as their average and sum on the third page
#9-19-09
#Turn off buffering
$| = 1;
#Set content type
print "Content-Type: text/html\n\n";
#Check to see if the user submitted the form
#if so the CONTENT_LENGTH must be greater than 0
#or the query string will be full
if ($ENV{'CONTENT_LENGTH'} == 0 && $ENV{'QUERY_STRING'} eq "")
{
#dislay the form in the browser
&sendForm;
exit;
}
#Get the form input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Put the buffer into an array and split the values with an &
@pairs = split(/&/, $buffer);
#Fix any characters thay may need replacing to display properly
#Code from scott2.cgi line 70
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9])([a-fA-F0-9])/pack("C", hex($1.$2))/eg;
if ( $contents{$name} eq "") {
$contents{$name} = $value;
}
else {
$contents{$name} .= " " . $value ;
}
}
#Check to see if it was a POST Submission
if ($ENV{'CONTENT_LENGTH'} > 0 ) {
#Print the input boxes for the user to fill in and submit to the screen
print "<h3>Thank you for submission!</h3> <br />";
print "You have selected to use $contents{'elems'} elements! <br />";
print "Please input your desired numbers to add and average in the fields below.<br /><br />";
#Display the element fields that the user requested
#Code from scott2.cgi line 112, with small modifications
print "<form>";
$ss = "0";
while ($ss < $contents{'elems'} ) {
$ss++;
$tmpx= "ELEM" .$ss;
print "<input type=text name=$tmpx size=5 value=10><br />\n";
}
print "<br /><input type=submit name=process value=Process><br />\n";
print "</form>";
#Check if Submit was used if so display the users IP Address
if ($contents{'submit'} eq "Submit" )
{
print "<b>Your IP Address is:</b> $ENV{'REMOTE_ADDR'} <br /> ";
}
}
#Check to see if it was a GET submission
elsif ($ENV{'QUERY_STRING'} ne "") {
#Store the contents of the QUERY_STRING in an array
@elemarray = split(/&/,$ENV{'QUERY_STRING'});
#Print the array contents for debugging
#print "ELEMARRAY: @elemarray<br /><br />";
#Print the array out line by line
#print "$_<br />" for @elemarray;
#Print the inputed numbers to the screen
print "You have entered the following numbers:<br />";
for ( @elemarray ) {
push (@elems, $2) if /(\d+)=(\d+)/;
}
print "<br /><br />";
print "<b>$_<br /></b>" for @elems;
#Print the contents of the QUERY_STRING for debugging
#print "QUERY_STRING: $ENV{'QUERY_STRING'}<br /><br />";
#Print the sum of the numbers to the screen
print "<br />The sum of your numbers is:<br /><br />";
foreach $elem( @elems ) {
$total += $elem;
$sum++;
}
print "<b>$total</b><br /><br />";
#Print the average of the numbers to the screen
print "The average of your numbers is:<br /><br />";
$average = $total / $sum;
print "<b><i>$average</b></i><br />";
}
#sendForm subsection
sub sendForm
{
#The form html code
print <<F1;
<html>
<head>
<title>Ronny L. Bull - Week 5 Project</title>
</head>
<body>
<h3>Elemement Request Form:</h3>
<form method="POST" action="week5.cgi">
Please enter the number of elements you would like to use:<br /><br />
<input type="text" name="elems" value="3" size="5"><br /><br />
<input type="submit" name="submit" value="Submit">
<input type="reset" value="Defaults">
</form>
</body>
</html>
F1
}
Add & Average (Queue Version):
#!/usr/bin/perl
#Ronny L. Bull - CS351
#CSCREDIT - queue
#Week 5 project - Develop a CGI application that:
#Requests a number of elements to add and average on the first page
#Outputs blanks for each element to be filled in on the second page
#Prints the inputs as well as their average and sum on the third page
#9-19-09
#Turn off buffering
$| = 1;
#Set content type
print "Content-Type: text/html\n\n";
#Check to see if the user submitted the form
#if so the CONTENT_LENGTH must be greater than 0
#or the query string will be full
if ($ENV{'CONTENT_LENGTH'} == 0 && $ENV{'QUERY_STRING'} eq "")
{
#dislay the form in the browser
&sendForm;
exit;
}
#Get the form input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Put the buffer into an array and split the values with an &
@pairs = split(/&/, $buffer);
#Fix any characters thay may need replacing to display properly
#Code from scott2.cgi line 70
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9])([a-fA-F0-9])/pack("C", hex($1.$2))/eg;
if ( $contents{$name} eq "") {
$contents{$name} = $value;
}
else {
$contents{$name} .= " " . $value ;
}
}
#Check to see if it was a POST Submission
if ($ENV{'CONTENT_LENGTH'} > 0 ) {
#Print the input boxes for the user to fill in and submit to the screen
print "<h3>Thank you for submission!</h3> <br />";
print "You have selected to use $contents{'elems'} elements! <br />";
print "Please input your desired numbers to add and average in the fields below.<br /><br />";
#Display the element fields that the user requested
#Code from scott2.cgi line 112, with small modifications
print "<form>";
$ss = "0";
while ($ss < $contents{'elems'} ) {
$ss++;
$tmpx= "ELEM" .$ss;
print "<input type=text name=$tmpx size=5 value=10><br />\n";
}
print "<br /><input type=submit name=process value=Process><br />\n";
print "</form>";
#Check if Submit was used if so display the users IP Address
if ($contents{'submit'} eq "Submit" )
{
print "<b>Your IP Address is:</b> $ENV{'REMOTE_ADDR'} <br /> ";
}
}
#Check to see if it was a GET submission
elsif ($ENV{'QUERY_STRING'} ne "") {
#Store the contents of the QUERY_STRING in a queue
unshift(@elemarray, split(/&/,$ENV{'QUERY_STRING'}));
#Print the array contents for debugging
#print "ELEMARRAY: @elemarray<br /><br />";
#Print the array out line by line
#print "$_<br />" for @elemarray;
#Print the inputed numbers to the screen
print "You have entered the following numbers:<br />";
for ( @elemarray ) {
push (@elems, $2) if /(\d+)=(\d+)/;
}
print "<br /><br />";
print "<b>$_<br /></b>" for @elems;
#Print the contents of the QUERY_STRING for debugging
#print "QUERY_STRING: $ENV{'QUERY_STRING'}<br /><br />";
#Print the sum of the numbers to the screen
print "<br />The sum of your numbers is:<br /><br />";
foreach $elem( @elems ) {
$total += $elem;
$sum++;
}
print "<b>$total</b><br /><br />";
#Print the average of the numbers to the screen
print "The average of your numbers is:<br /><br />";
$average = $total / $sum;
print "<b><i>$average</b></i><br />";
}
#sendForm subsection
sub sendForm
{
#The form html code
print <<F1;
<html>
<head>
<title>Ronny L. Bull - Week 5 Project</title>
</head>
<body>
<h3>Elemement Request Form:</h3>
<form method="POST" action="CSCRED_week5.cgi">
Please enter the number of elements you would like to use:<br /><br />
<input type="text" name="elems" value="3" size="5"><br /><br />
<input type="submit" name="submit" value="Submit">
<input type="reset" value="Defaults">
</form>
</body>
</html>
F1
}
Hidden Inputs:
#!/usr/bin/perl
#Ronny L. Bull - CS351
#Week 6 project - Hidden Inputs
#Develop a CGI program that uses hidden inputs and HTML tables
#Date: 9-24-2009
#Turn off buffering
$| = 1;
#Set content type
print "Content-Type: text/html\n\n";
#Check to see if the user submitted the form
#if so the CONTENT_LENGTH must be greater than 0
#or the QUERY_STRING will be full
if ($ENV{'CONTENT_LENGTH'} == 0 && $ENV{'QUERY_STRING'} eq "")
{
#Send the form to the browser
&sendForm1;
exit;
}
#Get the form input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Put the buffer into an array and slit the values with an &
@pairs = split(/&/, $buffer);
#Fix any characters that may need replacing to display properly
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9])([a-fA-F0-9])/pack("C", hex($1.$2))/eg;
if ( $contents{$name} eq "") {
$contents{$name} = $value;
}
else {
$contents{$name} .= " " . $value ;
}
}
#Check to see if the user submitted form1
#If so send form2
if ( $contents{'form'} eq "form1" )
{
&sendForm2;
exit;
}
#Check to see if the user entered a value besides 0 for columns
#Check to see if the user submitted form2
#If so send the table and form2 again
if ( $contents{'form'} eq "form2" && $contents{'col'} > 0 )
{
#for debugging
#print " The form is: $contents{'form'}<br /> ";
#print " The init value is: $contents{'init'}<br /> ";
#print " The incr value is: $contents{'incr'}<br /> ";
#print " The num value is: $contents{'num'}<br /> ";
#print " The col value is: $contents{'col'}<br /><br /> ";
#end debugging
#Send the table
print "<h3>Your Table</h3>";
print "<table border=1>\n";
#debugging
#print "Num Rows: ".($contents{'num'}/$contents{'col'})."\n";
my $value=$contents{'init'};
my $cntr=1;
for($row=0; $row < ($contents{'num'}/$contents{'col'}); $row++)
{
print "<tr>\n";
for($col=0; $col < ($contents{'col'} > $contents{'num'} ? $contents{'num'} : $contents{'col'}); $col++)
{
if($cntr > $contents{'num'})
{
print "<td> </td>\n";
}
else
{
print "<td>$value</td>\n";
$value += $contents{'incr'};
}
$cntr++;
}
print "</tr>";
}
print "</table>";
#Send form2 again
&sendForm2;
exit;
}
else
{
print "<b>Please enter a value greater than <i>0</i> for number of columns!</b>";
&sendForm2;
exit;
}
####################
#sendForm subsection
####################
sub sendForm1
{
print <<F1;
<html>
<head>
<title>Ronny L. Bull - Week 6 Project (Hidden)</title>
</head>
<body>
<h3>Please fill in the values below:</h3>
<form method="POST" action="week6_hidden.cgi">
Initial Number: <input type="text" name="init" value="4" size="5"><br /><br />
Increment By: <input type="text" name="incr" value="2" size="5"><br /><br />
How Many Times: <input type="text" name="num" value="10" size="5"><br /><br />
<input type="hidden" name="form" value="form1">
<input type="submit" name="submit" value="Submit">
<input type="reset" value="Defaults">
</form>
</body>
</html>
F1
}
sub sendForm2
{
print <<F2;
<html>
<head>
<title>Ronny L. Bull - Week 6 Project (Hidden)</title>
</head>
<body>
<h3>Plase fill in the values below:</h3>
<form method="POST" action="week6_hidden.cgi">
How many columns would you like: <input type="text" name="col" value="5" size="5"><br /><br />
<input type="hidden" name="init" value="$contents{'init'}">
<input type="hidden" name="incr" value="$contents{'incr'}">
<input type="hidden" name="num" value="$contents{'num'}">
<input type="hidden" name="form" value="form2">
<input type="submit" name="submit" value="Create Table">
<input type="reset" value="Defaults">
</form>
</body>
</html>
F2
}
Hidden Inputs (2D Array Version):
#!/usr/bin/perl
#Ronny L. Bull - CS351
#CSCREDIT - 2D Array
#Week 6 project - Hidden Inputs
#Develop a CGI program that uses hidden inputs and HTML tables
#Date: 9-24-2009
#Turn off buffering
$| = 1;
#Set content type
print "Content-Type: text/html\n\n";
#Check to see if the user submitted the form
#if so the CONTENT_LENGTH must be greater than 0
#or the QUERY_STRING will be full
if ($ENV{'CONTENT_LENGTH'} == 0 && $ENV{'QUERY_STRING'} eq "")
{
#Send the form to the browser
&sendForm1;
exit;
}
#Get the form input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Put the buffer into an array and slit the values with an &
@pairs = split(/&/, $buffer);
#Fix any characters that may need replacing to display properly
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9])([a-fA-F0-9])/pack("C", hex($1.$2))/eg;
if ( $contents{$name} eq "") {
$contents{$name} = $value;
}
else {
$contents{$name} .= " " . $value ;
}
}
#Check to see if the user submitted form1
#If so send form2
if ( $contents{'form'} eq "form1" )
{
&sendForm2;
exit;
}
#Check to see if the user entered a value besides 0 for columns
#Check to see if the user submitted form2
#If so send the table and form2 again
if ( $contents{'form'} eq "form2" && $contents{'col'} > 0 )
{
#for debugging
#print " The form is: $contents{'form'}<br /> ";
#print " The init value is: $contents{'init'}<br /> ";
#print " The incr value is: $contents{'incr'}<br /> ";
#print " The num value is: $contents{'num'}<br /> ";
#print " The col value is: $contents{'col'}<br /><br /> ";
#end debugging
#Send the table
print "<h3>Your Table</h3>";
print "<table border=1>\n";
#debugging
#print "Num Rows: ".($contents{'num'}/$contents{'col'})."\n";
my $value=$contents{'init'};
my $cntr=1;
#create 2d array
my @rows;
my @cols;
my @data = ([@rows],[@cols]);
for($row=0; $row < ($contents{'num'}/$contents{'col'}); $row++)
{
print "<tr>\n";
for($col=0; $col < ($contents{'col'} > $contents{'num'} ? $contents{'num'} : $contents{'col'}); $col++)
{
if($cntr > $contents{'num'})
{
print "<td> </td>\n";
}
else
{
#put value into 2d array
$data[$row][$col] = $value;
print "<td>$data[$row][$col]</td>\n";
$value += $contents{'incr'};
}
$cntr++;
}
print "</tr>";
}
print "</table>";
#Send form2 again
&sendForm2;
exit;
}
else
{
print "<b>Please enter a value greater than <i>0</i> for number of columns!</b>";
&sendForm2;
exit;
}
####################
#sendForm subsection
####################
sub sendForm1
{
print <<F1;
<html>
<head>
<title>Ronny L. Bull - Week 6 Project (Hidden)</title>
</head>
<body>
<h3>Please fill in the values below:</h3>
<form method="POST" action="CSCRED_week6_hidden.cgi">
Initial Number: <input type="text" name="init" value="4" size="5"><br /><br />
Increment By: <input type="text" name="incr" value="2" size="5"><br /><br />
How Many Times: <input type="text" name="num" value="10" size="5"><br /><br />
<input type="hidden" name="form" value="form1">
<input type="submit" name="submit" value="Submit">
<input type="reset" value="Defaults">
</form>
</body>
</html>
F1
}
sub sendForm2
{
print <<F2;
<html>
<head>
<title>Ronny L. Bull - Week 6 Project (Hidden)</title>
</head>
<body>
<h3>Plase fill in the values below:</h3>
<form method="POST" action="CSCRED_week6_hidden.cgi">
How many columns would you like: <input type="text" name="col" value="5" size="5"><br /><br />
<input type="hidden" name="init" value="$contents{'init'}">
<input type="hidden" name="incr" value="$contents{'incr'}">
<input type="hidden" name="num" value="$contents{'num'}">
<input type="hidden" name="form" value="form2">
<input type="submit" name="submit" value="Create Table">
<input type="reset" value="Defaults">
</form>
</body>
</html>
F2
}
Path Info:
#!/usr/bin/perl
#Ronny L. Bull - CS351
#Week 6 project - PATH_INFO
#Develop a CGI program that uses PATH_INFO and HTML tables
#Date: 9-27-2009
#Turn off buffering
$| = 1;
#Set content type
print "Content-Type: text/html\n\n";
#Check to see if the user submitted the form
#if so the CONTENT_LENGTH must be greater than 0
#or the QUERY_STRING will be full
if ($ENV{'CONTENT_LENGTH'} == 0 && $ENV{'QUERY_STRING'} eq "")
{
#Send the form to the browser
&sendForm1;
exit;
}
#Get the form input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Put the buffer into an array and slit the values with an &
@pairs = split(/&/, $buffer);
#Fix any characters that may need replacing to display properly
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9])([a-fA-F0-9])/pack("C", hex($1.$2))/eg;
if ( $contents{$name} eq "") {
$contents{$name} = $value;
}
else {
$contents{$name} .= " " . $value ;
}
}
#Check to see if the user submitted form1
#If so send form2
if ( $contents{'submit'} eq "Submit" )
{
&sendForm2;
exit;
}
#Check to see if the user entered a value besides 0 for columns
#Check to see if the user submitted form2
#If so send the table and form2 again
if ( $contents{'submit'} eq "Create Table" && $contents{'col'} > 0 )
{
#for debugging
#print " The form is: $contents{'form'}<br /> ";
#print " The init value is: $contents{'init'}<br /> ";
#print " The incr value is: $contents{'incr'}<br /> ";
#print " The num value is: $contents{'num'}<br /> ";
#print " The col value is: $contents{'col'}<br /><br /> ";
#print " PATH_INFO is: $ENV{'PATH_INFO'}<br />";
#end debugging
#Split PATH_INFO and store the values in variables
($null, $init, $incr, $num) = split("/",$ENV{'PATH_INFO'});
#PATH_INFO Split debugging
#print "init is: $init<br />";
#print "incr is: $incr<br />";
#print "num is: $num<br />";
#Send the table
print "<h3>Your Table</h3>";
print "<table border=1>\n";
#debugging
#print "Num Rows: ".($num/$contents{'col'})."\n";
my $value=$init;
my $cntr=1;
for($row=0; $row < ($num/$contents{'col'}); $row++)
{
print "<tr>\n";
for($col=0; $col < ($contents{'col'} > $num ? $num : $contents{'col'}); $col++)
{
if($cntr > $num)
{
print "<td> </td>\n";
}
else
{
print "<td>$value</td>\n";
$value += $incr;
}
$cntr++;
}
print "</tr>";
}
print "</table>";
#Send form2 again
&sendForm2;
exit;
}
else
{
print "<b>Please enter a value greater than <i>0</i> for number of columns!</b>";
&sendForm2;
exit;
}
####################
#sendForm subsection
####################
sub sendForm1
{
print <<F1;
<html>
<head>
<title>Ronny L. Bull - Week 6 Project (PATH_INFO)</title>
</head>
<body>
<h3>Please fill in the values below:</h3>
<form method="POST" action="week6_pathinfo.cgi">
Initial Number: <input type="text" name="init" value="4" size="5"><br /><br />
Increment By: <input type="text" name="incr" value="2" size="5"><br /><br />
How Many Times: <input type="text" name="num" value="10" size="5"><br /><br />
<!-- <input type="hidden" name="form" value="form1"> -->
<input type="submit" name="submit" value="Submit">
<input type="reset" value="Defaults">
</form>
</body>
</html>
F1
}
sub sendForm2
{
print <<F2;
<html>
<head>
<title>Ronny L. Bull - Week 6 Project (PATH_INFO)</title>
</head>
<body>
<h3>Plase fill in the values below:</h3>
<form method="POST" action="week6_pathinfo.cgi/$contents{'init'}/$contents{'incr'}/$contents{'num'}/$null">
How many columns would you like: <input type="text" name="col" value="5" size="5"><br /><br />
<!-- <input type="hidden" name="form" value="form2"> -->
<input type="submit" name="submit" value="Create Table">
<input type="reset" value="Defaults">
</form>
</body>
</html>
F2
}
Path Info (2D Array Version):
#!/usr/bin/perl
#Ronny L. Bull - CS351
#CSCREDIT - 2d Array
#Week 6 project - PATH_INFO
#Develop a CGI program that uses PATH_INFO and HTML tables
#Date: 9-27-2009
#Turn off buffering
$| = 1;
#Set content type
print "Content-Type: text/html\n\n";
#Check to see if the user submitted the form
#if so the CONTENT_LENGTH must be greater than 0
#or the QUERY_STRING will be full
if ($ENV{'CONTENT_LENGTH'} == 0 && $ENV{'QUERY_STRING'} eq "")
{
#Send the form to the browser
&sendForm1;
exit;
}
#Get the form input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Put the buffer into an array and slit the values with an &
@pairs = split(/&/, $buffer);
#Fix any characters that may need replacing to display properly
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9])([a-fA-F0-9])/pack("C", hex($1.$2))/eg;
if ( $contents{$name} eq "") {
$contents{$name} = $value;
}
else {
$contents{$name} .= " " . $value ;
}
}
#Check to see if the user submitted form1
#If so send form2
if ( $contents{'submit'} eq "Submit" )
{
&sendForm2;
exit;
}
#Check to see if the user entered a value besides 0 for columns
#Check to see if the user submitted form2
#If so send the table and form2 again
if ( $contents{'submit'} eq "Create Table" && $contents{'col'} > 0 )
{
#for debugging
#print " The form is: $contents{'form'}<br /> ";
#print " The init value is: $contents{'init'}<br /> ";
#print " The incr value is: $contents{'incr'}<br /> ";
#print " The num value is: $contents{'num'}<br /> ";
#print " The col value is: $contents{'col'}<br /><br /> ";
#print " PATH_INFO is: $ENV{'PATH_INFO'}<br />";
#end debugging
#Split PATH_INFO and store the values in variables
($null, $init, $incr, $num) = split("/",$ENV{'PATH_INFO'});
#PATH_INFO Split debugging
#print "init is: $init<br />";
#print "incr is: $incr<br />";
#print "num is: $num<br />";
#Send the table
print "<h3>Your Table</h3>";
print "<table border=1>\n";
#debugging
#print "Num Rows: ".($num/$contents{'col'})."\n";
my $value=$init;
my $cntr=1;
#Create 2D array
my @rows;
my @cols;
my @data = ([@rows],[@cols]);
for($row=0; $row < ($num/$contents{'col'}); $row++)
{
print "<tr>\n";
for($col=0; $col < ($contents{'col'} > $num ? $num : $contents{'col'}); $col++)
{
if($cntr > $num)
{
print "<td> </td>\n";
}
else
{
#put value into 2d array
$data[$row][$col] = $value;
print "<td>$data[$row][$col]</td>\n";
$value += $incr;
}
$cntr++;
}
print "</tr>";
}
print "</table>";
#Send form2 again
&sendForm2;
exit;
}
else
{
print "<b>Please enter a value greater than <i>0</i> for number of columns!</b>";
&sendForm2;
exit;
}
####################
#sendForm subsection
####################
sub sendForm1
{
print <<F1;
<html>
<head>
<title>Ronny L. Bull - Week 6 Project (PATH_INFO)</title>
</head>
<body>
<h3>Please fill in the values below:</h3>
<form method="POST" action="CSCRED_week6_pathinfo.cgi">
Initial Number: <input type="text" name="init" value="4" size="5"><br /><br />
Increment By: <input type="text" name="incr" value="2" size="5"><br /><br />
How Many Times: <input type="text" name="num" value="10" size="5"><br /><br />
<!-- <input type="hidden" name="form" value="form1"> -->
<input type="submit" name="submit" value="Submit">
<input type="reset" value="Defaults">
</form>
</body>
</html>
F1
}
sub sendForm2
{
print <<F2;
<html>
<head>
<title>Ronny L. Bull - Week 6 Project (PATH_INFO)</title>
</head>
<body>
<h3>Plase fill in the values below:</h3>
<form method="POST" action="CSCRED_week6_pathinfo.cgi/$contents{'init'}/$contents{'incr'}/$contents{'num'}/$null">
How many columns would you like: <input type="text" name="col" value="5" size="5"><br /><br />
<!-- <input type="hidden" name="form" value="form2"> -->
<input type="submit" name="submit" value="Create Table">
<input type="reset" value="Defaults">
</form>
</body>
</html>
F2
}
Line By Line File Viewer:
#!/usr/bin/perl
#Ronny L. Bull - CS351
#Week 7 project - File Line Viewer
#Date: 10-02-2009
#Turn off buffering
$| = 1;
#Set the counter
my $cnt = 0;
#Set content type
print "Content-Type: text/html\n\n";
#Check to see if the user submitted the form
#if so the CONTENT_LENGTH must be greater than 0
#or the QUERY_STRING will be full
if ($ENV{'CONTENT_LENGTH'} == 0 && $ENV{'QUERY_STRING'} eq "")
{
#Send the form to the browser
&sendForm;
exit;
}
#Get the form input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Put the buffer into an array and slit the values with an &
@pairs = split(/&/, $buffer);
#Fix any characters that may need replacing to display properly
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9])([a-fA-F0-9])/pack("C", hex($1.$2))/eg;
if ( $contents{$name} eq "") {
$contents{$name} = $value;
}
else {
$contents{$name} .= " " . $value ;
}
}
#Check to see if it was a POST Submission
if ($ENV{'CONTENT_LENGTH'} > 0) {
#Load the selected file into an array
open (FILE, "$contents{'file'}") || die("Could not open the file\n");
@infile = <FILE>;
#Get the total number of lines in the file, starts at 0
$linecnt = $#infile;
#add 1 to the linecount to get the total number of lines
$linecnt1 = $#infile+1;
#Set the counter
$cnt = $contents{'count'};
#Setup the direction counter
if ($contents{'dir'} eq "Forward" && $contents{'file'} eq $contents{'oldfile'} && $contents{'olddir'} eq "Forward")
{
$cnt++;
}
elsif ($contents{'dir'} eq "Reverse" && $contents{'file'} eq $contents{'oldfile'} && $contents{'olddir'} eq "Reverse")
{
$cnt--;
}
elsif ($contents{'dir'} eq "Forward" && $contents{'olddir'} eq "Reverse")
{
$cnt=0;
}
elsif ($contents{'dir'} eq "Reverse" && $contents{'olddir'} eq "Forward")
{
$cnt=$#infile;
}
elsif ($contents{'dir'} eq "Forward" && $contents{'file'} ne $contents{'oldfile'})
{
$cnt=0;
}
elsif ($contents{'dir'} eq "Reverse" && $contents{'file'} ne $contents{'oldfile'})
{
$cnt=$#infile;
}
if ($cnt < 0)
{
$cnt=0;
print "<b><i>You are already at the beginning of the file!</i></b> <br /><br />";
}
elsif ($cnt > $#infile)
{
$cnt=$#infile;
print "<b><i>You are already at the end of the file!</i></b> <br /><br />";
}
#The variable to hold the current line of text from the file
$out = $infile[$cnt];
#Print the current line number
print "<b>Line number:</b> $cnt <br />";
#Print the current line
print "<b>Line contents:</b> <i>$out</i> <br /><br />";
#Begin Debugging
#print "<b>Debugging Output</b><br /><br />";
#print "The file is: $contents{'file'}<br />";
#print "The direction is: $contents{'dir'}<br />";
#print "The old file is: $contents{'oldfile'}<br />";
#print "The old direction is: $contents{'olddir'}<br />";
#print "The file has $linecnt1 lines.<br />";
#print "The hidden count is: $contents{'count'} <br /><br />";
#print "The file contents are: <br /><br />";
#Print the file in the array line by line
#for $in (0 .. $#infile)
#{
# print $infile[$in], "<br />";
#}
#print "<br /><b>End of Debugging</b><br />";
#End of Debugging
#send the form
&sendForm;
exit;
}
####################
#sendForm subsection
####################
sub sendForm
{
#Form HTML
print <<F1;
<html>
<head>
<title>Ronny L. Bull - Week 7 Project</title>
</head>
<body>
<h3>Line By Line File Viewer</h3>
<form method="POST" action="week7_revised_10-26-2009.cgi">
Please select a file:<br />
<select name="file" single size=3>
F1
@select = ("William Blake","Socrates","Confucius");
foreach $select (@select)
{
print "<option";
if($select eq $contents{'file'})
{
print " selected";
}
print ">$select</option>";
}
print "</select><br /><br />";
print "What direction?<br /><br />";
@direction = ("Forward","Reverse");
foreach $direction (@direction)
{
print "<input type=radio name=dir value=$direction";
if($direction eq $contents{'dir'})
{
print " checked";
}
print " />$direction <br />";
}
print <<F1;
<input type="hidden" name="oldfile" value="$contents{'file'}">
<input type="hidden" name="olddir" value="$contents{'dir'}">
<input type="hidden" name="count" value="$cnt">
<br /><br />
<input type="submit" name="submit" value="Scan">
</form>
</body>
</html>
F1
}
Line By Line File Viewer (Hash Version):
#!/usr/bin/perl
#Ronny L. Bull - CS351
#CSCRED - Hash
#Week 7 project - File Line Viewer
#Date: 10-02-2009
#Turn off buffering
$| = 1;
#Set the counter
my $cnt = 0;
#Set content type
print "Content-Type: text/html\n\n";
#Check to see if the user submitted the form
#if so the CONTENT_LENGTH must be greater than 0
#or the QUERY_STRING will be full
if ($ENV{'CONTENT_LENGTH'} == 0 && $ENV{'QUERY_STRING'} eq "")
{
#Send the form to the browser
&sendForm;
exit;
}
#Get the form input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Put the buffer into an array and slit the values with an &
@pairs = split(/&/, $buffer);
#Fix any characters that may need replacing to display properly
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9])([a-fA-F0-9])/pack("C", hex($1.$2))/eg;
if ( $contents{$name} eq "") {
$contents{$name} = $value;
}
else {
$contents{$name} .= " " . $value ;
}
}
#Check to see if it was a POST Submission
if ($ENV{'CONTENT_LENGTH'} > 0) {
#Load the selected file into an array
open (FILE, "$contents{'file'}") || die("Could not open the file\n");
#@infile = <FILE>;
#load file into a hash table line by line
$i = 0;
while(<FILE>)
{
$line = $_;
chomp $line;
$hash_table{$i} = $line;
++$i;
}
close(FILE);
#Get the total number of lines in the file, starts at 0
#$linecnt = $#infile;
$linecnt = scalar keys %hash_table;
#add 1 to the linecount to get the total number of lines
#$linecnt1 = $#infile+1;
$linecnt1 = $linecnt + 1;
#Set the counter
$cnt = $contents{'count'};
#Setup the direction counter
if ($contents{'dir'} eq "Forward" && $contents{'file'} eq $contents{'oldfile'} && $contents{'olddir'} eq "Forward")
{
$cnt++;
}
elsif ($contents{'dir'} eq "Reverse" && $contents{'file'} eq $contents{'oldfile'} && $contents{'olddir'} eq "Reverse")
{
$cnt--;
}
elsif ($contents{'dir'} eq "Forward" && $contents{'olddir'} eq "Reverse")
{
$cnt=0;
}
elsif ($contents{'dir'} eq "Reverse" && $contents{'olddir'} eq "Forward")
{
$cnt=$linecnt;
}
elsif ($contents{'dir'} eq "Forward" && $contents{'file'} ne $contents{'oldfile'})
{
$cnt=0;
}
elsif ($contents{'dir'} eq "Reverse" && $contents{'file'} ne $contents{'oldfile'})
{
$cnt=$linecnt;
}
if ($cnt < 0)
{
$cnt=0;
print "<b><i>You are already at the beginning of the file!</i></b> <br /><br />";
}
elsif ($cnt > $linecnt)
{
$cnt=$linecnt;
print "<b><i>You are already at the end of the file!</i></b> <br /><br />";
}
#store the current line of the file
$out = $hash_table{$cnt};
#Print the current line number
print "<b>Line number:</b> $cnt <br />";
#Print the current line
print "<b>Line contents:</b> <i>$out</i> <br /><br />";
#Begin Debugging
#print "<b>Debugging Output</b><br /><br />";
#print "The file is: $contents{'file'}<br />";
#print "The direction is: $contents{'dir'}<br />";
#print "The old file is: $contents{'oldfile'}<br />";
#print "The old direction is: $contents{'olddir'}<br />";
#print "The file has $linecnt1 lines.<br />";
#print "The hidden count is: $contents{'count'} <br /><br />";
#print "The file contents are: <br /><br />";
#Print the file in the array line by line
#for $in (0 .. $#infile)
#{
# print $infile[$in], "<br />";
#}
#print "<br /><b>End of Debugging</b><br />";
#End of Debugging
#send the form
&sendForm;
exit;
}
####################
#sendForm subsection
####################
sub sendForm
{
#Form HTML
print <<F1;
<html>
<head>
<title>Ronny L. Bull - Week 7 Project</title>
</head>
<body>
<h3>Line By Line File Viewer</h3>
<form method="POST" action="CSCRED_week7.cgi">
Please select a file:<br />
<select name="file" single size=3>
F1
@select = ("William Blake","Socrates","Confucius");
foreach $select (@select)
{
print "<option";
if($select eq $contents{'file'})
{
print " selected";
}
print ">$select</option>";
}
print "</select><br /><br />";
print "What direction?<br /><br />";
@direction = ("Forward","Reverse");
foreach $direction (@direction)
{
print "<input type=radio name=dir value=$direction";
if($direction eq $contents{'dir'})
{
print " checked";
}
print " />$direction <br />";
}
print <<F1;
<input type="hidden" name="oldfile" value="$contents{'file'}">
<input type="hidden" name="olddir" value="$contents{'dir'}">
<input type="hidden" name="count" value="$cnt">
<br /><br />
<input type="submit" name="submit" value="Scan">
</form>
</body>
</html>
F1
}
Line By Line File Viewer (Cookie Version):
#!/usr/bin/perl
#
#Ronny L. Bull - CS351
#Week 8 project - File Line Viewer (Cookie Version)
#Date: 10-24-2009
#Turn off buffering
$| = 1;
#Set the counter
my $cnt = 0;
#create month array
@monthname = ("Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec");
#Set content type
print "Content-Type: text/html\n\n";
#Check to see if the user submitted the form
#if so the CONTENT_LENGTH must be greater than 0
#or the QUERY_STRING will be full
#or the cookie will be empty
if ($ENV{'CONTENT_LENGTH'} == 0 && $ENV{'QUERY_STRING'} eq "" && $ENV{'HTTP_COOKIE'} eq "")
{
#Send the form
&sendForm;
exit;
}
#Look for the Cookie and check to see if the user did not send a form in
#if so the cookie will be full
#the CONTENT_LENGTH will equal 0
#and the QUERY_STRING will be empty
if ($ENV{'CONTENT_LENGTH'} == 0 && $ENV{'QUERY_STRING'} eq "" && $ENV{'HTTP_COOKIE'} ne "")
{
#Call to getCookie function
&getCookie;
if ($contents{'file'} eq "" && $ENV{'HTTP_COOKIE'} ne "")
{
$contents{'file'} = $oldfile;
$contents{'dir'} = $olddir;
$cnt = $count;
}
#Call to fileArray function
&fileArray;
#Call the printLine function
&printLine;
#Send the form to the browser
&sendForm;
exit;
}
#Get the form input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Put the buffer into an array and split the values with an &
@pairs = split(/&/, $buffer);
#Fix any characters that may need replacing to display properly
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9])([a-fA-F0-9])/pack("C", hex($1.$2))/eg;
if ( $contents{$name} eq "") {
$contents{$name} = $value;
}
else {
$contents{$name} .= " " . $value ;
}
}
#Check to see if it was a POST Submission
#If so the CONTENT_LENGTH will be greater than 0
#Look for cookies
if ($ENV{'CONTENT_LENGTH'} > 0) {
#Call to getCookie
&getCookie;
#call to fileArray
&fileArray;
#Set the counter
$cnt = $count;
#Setup the direction counter
if ($contents{'dir'} eq "Forward" && $contents{'file'} eq $oldfile && $olddir eq "Forward")
{
$cnt++;
}
elsif ($contents{'dir'} eq "Reverse" && $contents{'file'} eq $oldfile && $olddir eq "Reverse")
{
$cnt--;
}
elsif ($contents{'dir'} eq "Forward" && $olddir eq "Reverse")
{
$cnt=0;
}
elsif ($contents{'dir'} eq "Reverse" && $olddir eq "Forward")
{
$cnt=$#infile;
}
elsif ($contents{'dir'} eq "Forward" && $contents{'file'} ne $oldfile)
{
$cnt=0;
}
elsif ($contents{'dir'} eq "Reverse" && $contents{'file'} ne $oldfile)
{
$cnt=$#infile;
}
if ($cnt < 0)
{
$cnt=0;
print "<b><i>You are already at the beginning of the file!</i></b> <br /><br />";
}
elsif ($cnt > $#infile)
{
$cnt=$#infile;
print "<b><i>You are already at the end of the file!</i></b> <br /><br />";
}
&printLine;
#Print the file in the array line by line
#for $in (0 .. $#infile)
#{
# print $infile[$in], "<br />";
#}
#print "<br /><b>End of Debugging</b><br />";
#End of Debugging
#send the form
&sendForm;
exit;
}
####################
#fileArray function
####################
sub fileArray
{
#Load the selected file into an array
open (FILE, "$contents{'file'}") || die("Could not open the file\n");
@infile = <FILE>;
#Get the total number of lines in the file, starts at 0
$linecnt = $#infile;
#add 1 to the linecount to get the total number of lines
$linecnt1 = $#infile+1;
}
####################
#printLine function
####################
sub printLine
{
#The variable to hold the current line of text from the file
$out = $infile[$cnt];
#Print the current line number
print "<b>Line number:</b> $cnt <br />";
#Print the current line
print "<b>Line contents:</b> <i>$out</i> <br /><br />";
}
####################
#getCookie function
####################
sub getCookie
{
#Create a random ID for the cookie using the PID and the time
$random = $$ + time;
#Create expire time for cookie
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
$year="2020";
$expires = sprintf "%d-%s-%d %d:%d:%d",
$mday,$monthname[$mon],$year,$hour,$min+5,$sec;
$mytmp="newcookie=$random";
#Read in any available cookies and split them
$cookie = $ENV{'HTTP_COOKIE'};
@cookiepairs = split (';', $cookie);
foreach $pair (@cookiepairs)
{
($cookiename, $cookievalue) = split('=', $pair);
$cookiename =~ s/^ //; #Remove spaces at beginning
if ($cookiename eq "oldfile")
{
$oldfile = $cookievalue;
}
if ($cookiename eq "olddir")
{
$olddir = $cookievalue;
}
if ($cookiename eq "count")
{
$count = $cookievalue;
}
}
}
####################
#sendForm subsection
####################
sub sendForm
{
#Form HTML
print <<F1;
<html>
<head>
<!-- Set the cookies -->
<meta http-equiv="Set-Cookie" content="$mytmp; expires=$expires GMT">
<meta http-equiv="Set-Cookie" content="oldfile=$contents{'file'}; expires=$expires GMT">
<meta http-equiv="Set-Cookie" content="olddir=$contents{'dir'}; expires=$expires GMT">
<meta http-equiv="Set-Cookie" content="count=$cnt; expires=$expires GMT">
<title>Ronny L. Bull - Week 7 Project</title>
</head>
<body>
<h3>Line By Line File Viewer</h3>
<form method="POST" action="week7_cookie.cgi">
Please select a file:<br />
<select name="file" single size=3>
F1
@select = ("William Blake","Socrates","Confucius");
foreach $select (@select)
{
print "<option";
if($select eq $contents{'file'})
{
print " selected";
}
print ">$select</option>";
}
print "</select><br /><br />";
print "What direction?<br /><br />";
@direction = ("Forward","Reverse");
foreach $direction (@direction)
{
print "<input type=radio name=dir value=$direction";
if($direction eq $contents{'dir'})
{
print " checked";
}
print " />$direction <br />";
}
print <<F1;
<br /><br />
<input type="submit" name="submit" value="Scan">
</form>
</body>
</html>
F1
}
Client Side Image Map:
<html> <img src="athf.jpg" usemap="#athf" border="0"> <map name="athf"> <area shape="rect" coords="0,100,160,310" href="http://web.cs.sunyit.edu/~bullr/cs351.php"> <area shape="rect" coords="175,200,295,430" href="http://web.cs.sunyit.edu/~bullr"> <area shape="circle" coords="382,212,63" href="http://web.cs.sunyit.edu/~bullr/contact.php"> </map> </html>
Server Side Image Map:
<html> <a href="http://www.cs.sunyit.edu/~bullr/cgi-bin/imagemap.cgi/athf"> <img src="/~bullr/cgi-bin/athf.jpg" ismap></a> </html>
athf.map
default http://www.cs.sunyit.edu/~bullr rect http://www.cs.sunyit.edu/~bullr/cs351 0,100 160,310 circle http://www.cs.sunyit.edu/~bullr/contact.php 382,211 448,211
GD Bar Graph:
#!/usr/bin/perl
#Ronny L. Bull - CS351
#Week 6 project - Hidden Inputs
#Develop a CGI program that uses hidden inputs and HTML tables
#Date: 9-24-2009
use CGI;
use GD::Graph::bars;
#Turn off buffering
$| = 1;
#Set content type
print "Content-Type: text/html\n\n";
#Check to see if the user submitted the form
#if so the CONTENT_LENGTH must be greater than 0
#or the QUERY_STRING will be full
if ($ENV{'CONTENT_LENGTH'} == 0 && $ENV{'QUERY_STRING'} eq "")
{
#Send the form to the browser
&sendForm1;
exit;
}
#Get the form input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Put the buffer into an array and slit the values with an &
@pairs = split(/&/, $buffer);
#Fix any characters that may need replacing to display properly
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9])([a-fA-F0-9])/pack("C", hex($1.$2))/eg;
if ( $contents{$name} eq "") {
$contents{$name} = $value;
}
else {
$contents{$name} .= " " . $value ;
}
}
#Check to see if the user submitted form1
#If so send form2
if ( $contents{'form'} eq "form1" )
{
&sendForm2;
exit;
}
#Check to see if the user entered a value besides 0 for columns
#Check to see if the user submitted form2
#If so send the table and form2 again
if ( $contents{'form'} eq "form2" && $contents{'col'} > 0 )
{
#for debugging
#print " The form is: $contents{'form'}<br /> ";
#print " The init value is: $contents{'init'}<br /> ";
#print " The incr value is: $contents{'incr'}<br /> ";
#print " The num value is: $contents{'num'}<br /> ";
#print " The col value is: $contents{'col'}<br /><br /> ";
#end debugging
#Send the table
print "<h3>Your Table</h3>";
print "<table border=1>\n";
#debugging
#print "Num Rows: ".($contents{'num'}/$contents{'col'})."\n";
#Create the table
my $value=$contents{'init'};
my $cntr=1;
for($row=0; $row < ($contents{'num'}/$contents{'col'}); $row++)
{
print "<tr>\n";
for($col=0; $col < ($contents{'col'} > $contents{'num'} ? $contents{'num'} : $contents{'col'}); $col++)
{
if($cntr > $contents{'num'})
{
print "<td> </td>\n";
}
else
{
$sum += $value;
print "<td>$value</td>\n";
$value += $contents{'incr'};
}
$cntr++;
}
#Debugging output of row sums
#print "The sum of row $row is $sum<br />";
#Create arrays for rows and sums
$sums[$row] = $sum;
$rows[$row] = $row;
#Reset sum to 0 for the next loop
$sum=0;
print "</tr>";
}
print "</table><br /><br />";
#Debugging output for arrays
#print "Sums array: @sums<br />";
#print "Rows array: @rows<br />";
#Create a 2d array for the bar graph data
my @data = ([@rows],[@sums]);
#Create the graph dimensions
my $graph = GD::Graph::bars->new(500, 300);
#Create the graph labels
$graph->set(
x_label => 'Row',
y_label => 'Sum',
title => 'The Sum Of Each Row',
) or warn $graph->error;
#Plot the graph data
my $image = $graph->plot(\@data) or die $graph->error;
#Display the Graph
#print "Content-Type: image/png\n\n"; #Does not work cause already printed a content type
#print $image->png;
#Create a new temp file for the graph
open BAR, ">bar.png";
print BAR $image->png;
close BAR;
print "<img src=\"bar.png\">";
#Send form2 again
&sendForm2;
exit;
}
else
{
print "<b>Please enter a value greater than <i>0</i> for number of columns!</b>";
&sendForm2;
exit;
}
####################
#sendForm subsection
####################
sub sendForm1
{
print <<F1;
<html>
<head>
<title>Ronny L. Bull - Week 6 Project (Hidden)</title>
</head>
<body>
<h3>Please fill in the values below:</h3>
<form method="POST" action="bonus8.cgi">
Initial Number: <input type="text" name="init" value="4" size="5"><br /><br />
Increment By: <input type="text" name="incr" value="2" size="5"><br /><br />
How Many Times: <input type="text" name="num" value="10" size="5"><br /><br />
<input type="hidden" name="form" value="form1">
<input type="submit" name="submit" value="Submit">
<input type="reset" value="Defaults">
</form>
</body>
</html>
F1
}
sub sendForm2
{
print <<F2;
<html>
<head>
<title>Ronny L. Bull - Bonus Project 8</title>
</head>
<body>
<h3>Plase fill in the values below:</h3>
<form method="POST" action="bonus8.cgi">
How many columns would you like: <input type="text" name="col" value="5" size="5"><br /><br />
<input type="hidden" name="init" value="$contents{'init'}">
<input type="hidden" name="incr" value="$contents{'incr'}">
<input type="hidden" name="num" value="$contents{'num'}">
<input type="hidden" name="form" value="form2">
<input type="submit" name="submit" value="Create Table">
<input type="reset" value="Defaults">
</form>
</body>
</html>
F2
}
Biggest File:
#!/usr/bin/perl
#Ronny L. Bull - CS351
#Bonus 9 - Biggest file
#Date 11-7-2009
#Turn off buffering
$| = 1;
#Set content type
print "Content-Type: text/html\n\n";
#Create a random number
$random = rand(100);
#Get the form input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Put the buffer into an array and split the values with an &
@pairs = split(/&/, $buffer);
#Fix any characters that may need replacing to display properly
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9])([a-fA-F0-9])/pack("C", hex($1.$2))/eg;
if ( $contents{$name} eq "") {
$contents{$name} = $value;
}
else {
$contents{$name} .= " " . $value ;
}
}
#Create an array from the ls command
@LS=`ls *.txt`;
chomp(@LS);
#Add a file to the array and directory
if ($contents{'aorsub'} eq "Add File")
{
#add to array
push (@LS, $contents{'aor'});
#add to directory
open FILE, ">$contents{'aor'}";
print FILE "Prepare for a random amount of x's and y's!\n";
print FILE "x" x $random;
print FILE "y" x $random;
close FILE;
}
#Delete a file from the array and directory
elsif ($contents{'aorsub'} eq "Remove File")
{
$num = scalar(@LS);
for($i=0;$i<=$num;$i++)
{
if($LS[$i] eq $contents{'aor'})
{
#Delete from the array
delete $LS[$i];
#Delete from the directory
unlink($contents{'aor'});
}
}
}
#Put the selected files into an array
@FILES = split (/ /,$contents{'file_list'});
#Find the size of the array
$cnt = $#FILES;
#Test for the biggest file
foreach $file (@FILES)
{
if ($bigfile eq '')
{
$bigfile = $file;
$bigsize = -s $bigfile;
}
else
{
$filename = $file;
$filesize = -s $filename;
$bigsize = -s $bigfile;
if ($filesize > $bigsize)
{
$bigfile = $file;
}
}
}
&sendForm;
exit;
####################
#sendForm subsection
####################
sub sendForm
{
#Form HTML
print <<F1;
<html>
<head>
<title>Ronny L. Bull - Bonus 9 - Biggest File</title>
</head>
<body>
<h3>Please select the text files you wish to compare below</h3>
F1
print "<b>File List:</b><br />";
print <<F1;
<form method="POST" action="bonus9.cgi">
<select name="file_list" multiple>
F1
#Build the select from the ls array
foreach $option (@LS)
{
if ($option eq $FILES[0])
{
print "<option value='$option' selected >$option</option>";
#Remove the first element of the FILES array
shift(@FILES);
}
else
{
print "<option value='$option'>$option</option>";
}
}
print "</select><br /><br />";
print <<F1;
<input type="hidden" name="selected_files" value="@FILES">
<input type="submit" name="compare" value="Biggest"><br /><br />
F1
if ($contents{'compare'} eq "Biggest")
{
print "<b>The biggest file is:</b> <a href=$bigfile>$bigfile</a><br /><br />";
}
print <<F1;
<font color="red"><b>PLEASE NOTE:</b></font> This program is <font color="red"><b>DESTRUCTIVE!!!</b></font><br />
It really does add and delete the <b>.txt</b> files from the directory!<br />
If there are no files in the list when you start this program feel free to add some!<br /><br />
When adding a new file the program will create a file with a random<br />
amount of <i>x's</i> and <i>y's</i> in it to vary the file size of newly created files.<br /><br />
<b>Enter the name of the file you would like to add or remove from the list</b><br />
<i>The file must end with the <b>.txt</b> extension</i><br />
<input type="text" name="aor"><br />
<input type="submit" name="aorsub" value="Add File">
<input type="submit" name="aorsub" value="Remove File">
</form>
</body>
</html>
F1
}
Graded Exam:
#!/usr/bin/perl
#Ronny L. Bull - CS351
#Bonus 10 project - Graded Exam Part 1
#11-25-09
#Turn off buffering
$| = 1;
#Set content type
print "Content-Type: text/html\n\n";
#Check to see if the user submitted the form, if so the CONTENT_LENGTH
#must be greater than 0
if ($ENV{'CONTENT_LENGTH'} == 0 )
{
#dislay the form in the browser
&sendForm;
exit;
}
#Get the form input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Put the buffer into an array and split the values with an &
@pairs = split(/&/, $buffer);
#Fix any characters thay may need replacing to display properly
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9])([a-fA-F0-9])/pack("C", hex($1.$2))/eg;
if ( $contents{$name} eq "") {
$contents{$name} = $value;
}
else {
$contents{$name} .= " " . $value ;
}
}
#Check if the default submit button was used
if ($contents{'submit'} eq "Submit" )
{
print "<b>Here are your results $contents{'name'}:</b> <br /><br /><br /> ";
#store the student exam answers into an array
@exam = ($contents{'text1'}, $contents{'text2'}, $contents{'tf1'}, $contents{'tf2'}, $contents{'tf3'}, $contents{'mc1'}, $contents{'mc2'}, $contents{'mc3'});
#Open the answer key file and store it in an array
open(ANSWERS, "answerkey.txt");
@answers = <ANSWERS>;
close(ANSWERS);
#Print the answers array for debuggin
#print "Answers:<br /><br />";
#foreach(@answers)
# {
# print $_;
# print "<br />";
# }
#print "<br />";
#Print the student exam answers
print "Exam Results:<br /><br />";
my $i = 0;
foreach(@exam)
{
#open a new file named after the user, and store the users answers in it.
open EXAM, ">>$contents{'name'}" or die "can't open file $!";
print EXAM $_;
print EXAM "\n";
close EXAM;
#answers array element
chomp ($answers[$i]);
my $ans = $answers[$i];
#Debugging
#print $ans;
#student exam array elment
my $stu = $_;
print $stu;
#Compare answers and print result
#Debug Output
#print "ans=[$ans]\nstu=[$stu]\n";
if($stu eq $ans)
{
print " -<font color = green> Correct</font><br /><br />";
}
else
{
print " -<font color = red> Incorrect:</font> The correct answer is $ans<br /><br />";
}
++$i;
print "<br />";
}
}
#sendForm subsection
sub sendForm
{
#The form html code
print <<F1;
<html>
<head>
<title>Ronny L. Bull - Graded Exam</title>
</head>
<body>
<form method="POST" action="bonus10.cgi">
<b>Name:</b><br />
<input type="text" name="name"><br /><br />
<b>Direct Input:</b><br /><br />
<b>2 + 2 is:</b><br />
<input type="text" name="text1" size="4"><br /><br />
<b>3 x 3 is:</b><br />
<input type="text" name="text2" size="4"><br /><br /><br />
<b>True or False:</b><br /><br />
<b>You can put an elephant into a clown car:</b><br />
<input type="radio" name="tf1" value="True" /> True<br />
<input type="radio" name="tf1" value="False"/> False<br /><br />
<b>The sky is blue:</b><br />
<input type="radio" name="tf2" value="True" /> True<br />
<input type="radio" name="tf2" value="False"/> False<br /><br />
<b>A pot of gold exists at the end of each rainbow:</b><br />
<input type="radio" name="tf3" value="True" /> True<br />
<input type="radio" name="tf3" value="False"/> False<br /><br />
<b>Which of the following is a fish:</b><br />
<input type="radio" name="mc1" value="A" /> A. Walrus<br />
<input type="radio" name="mc1" value="B"/> B. Whale<br />
<input type="radio" name="mc1" value="C" /> C. Carp<br />
<input type="radio" name="mc1" value="D"/> D. Sea Lion<br /><br />
<b>Which of the following was a President:</b><br />
<input type="radio" name="mc2" value="A" /> A. Mickey Mouse<br />
<input type="radio" name="mc2" value="B"/> B. Daffy Duck<br />
<input type="radio" name="mc2" value="C" /> C. Ronald Regan<br />
<input type="radio" name="mc2" value="D"/> D. Goofy<br /><br />
<b>Which of the following is a solid:</b><br />
<input type="radio" name="mc3" value="A" /> A. Propane<br />
<input type="radio" name="mc3" value="B"/> B. Gasoline<br />
<input type="radio" name="mc3" value="C" /> C. Hydrogen<br />
<input type="radio" name="mc3" value="D"/> D. Mushroom<br /><br />
<!-- <b>Essay Question:</b><br />
<textarea name="essay" rows="7" cols="60">Type your essay here.</textarea><br /><br /> -->
<input type="submit" name="submit" value="Submit">
<input type="reset" value="Defaults">
</form>
</body>
</html>
F1
}
