Update script in Perl

This writes out a new runningtotal.shtml file. With hindsight, a cgi include could just read a value in from a file as part of the include file.

cgi-bin/updatetotal.pl

[host1] $ ~/cgi-bin/updatetotal.pl
#!/usr/bin/perl
#written Andrew Stringer 27/08/2008
#to process update to total cash raised.
 
use strict;
use warnings;
 
use CGI;
 
my $password="handel";
my $inputpassword=' ';
my $target="150000";
 
my $query = new CGI;
$inputpassword = $query->param("password");
my $total = $query->param("total");
 
if ($inputpassword eq $password)
{
#check if input amount is numeric (\D is any digit except a decimal digit)
if ($total =~ m/\D,/)
{
print "Content-type: text/html\n\n";
 
print <<ENDTEXT4 ;
<html>
<head>
<title>Error</title>
</head>
<body>
Total raised (&pound;$total) must be numbers only.
<br>
</body>
</html>
ENDTEXT4
exit (1);
}
 
#check if input ammount is greater than the target
if ($total > $target )
{
print "Content-type: text/html\n\n";
 
print <<ENDTEXT5 ;
<html>
<head>
<title>Error</title>
</head>
<body>
Total raised (&pound;$total) must be less than the target amount (&pound;$target).
<br>
</body>
</html>
ENDTEXT5
exit (1);
}
 
 
 
 
 
#write to file
open RUNNINGTOTAL, ">../http/runningtotal.shtml" or die "Cannot open file runningtotal.shtml for output." ;
 
print RUNNINGTOTAL  <<ENDTEXT1 ;
<div id="runningtotal">
<h2>Raised &amp; <br>Pledged so far:-</h2>
<p id="totalcash">
&pound; $total
</p>
<h2>Target &pound;150,000</h2>
</div>
ENDTEXT1
close RUNNINGTOTAL ;
 
#start output of html page
print "Content-type: text/html\n\n";
 
print <<ENDTEXT2 ;
<html>
<head>
<title>Total Updated</title>
</head>
<body>
Total updated and is now &pound;$total
<br>
<br>
<a href="/index.shtml">Return to main page</a>
<br>
</body>
</html>
ENDTEXT2
}
else
{
#start output of general error page
print "Content-type: text/html\n\n";
 
print <<ENDTEXT3 ;
<html>
<head>
<title>Error</title>
</head>
<body>
An error has occured.
<br>
</body>
</html>
ENDTEXT3
}
 
exit (0);

Form to submit values

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Update running total</title>
</head>
 
<body text="#000000" bgcolor="#ffffff" link="0000EE" vlink="#551A8B">
 
<form name="updatetotal" action="/cgi-bin/updatetotal.pl" method="post">
<table border="1" bgcolor="#dddddd">
<th>Update Running Total</th>
<tr>
 
<td>
Password:-
<input type="password" name="password" size="20" maxlength="20">
<br>
 
Running Total:- &pound;
<input type="text" name="total" size="6" maxlength="6">
<br>
 
<input type="Submit" value="Update">
</form>
</td>
</tr>
</table>
 
</body>
</html>

Counting ipaddresses

# more andrew/counter2.pl
#!/usr/bin/perl
#Program to count in ipaddresses
#written Andrew Stringer 16/08/2005
 
sub IP2DENARY
{
#get ipaddress passed to subroutine
my $ipaddress = shift;
 
#split into array on "."
my @octet = split /\./ , $ipaddress;
 
#convert each octet to hex, pad to 2 digits
my $hexip1= sprintf"%02x", $octet[0];
my $hexip2= sprintf"%02x", $octet[1];
my $hexip3= sprintf"%02x", $octet[2];
my $hexip4= sprintf"%02x", $octet[3];
 
#concatenate to one number
my $hexipaddress=$hexip1.$hexip2.$hexip3.$hexip4;
#convert to denary equivalent
my $denipaddress=hex($hexipaddress);
 
return $hexipaddress, $denipaddress;
}
 
sub DENARY2IP
{
#get denary equivalent of ipaddress passed to subroutine
my $denipaddress = shift;
 
#convert to hex and pad to 8 digits
my $hexip= sprintf"%08x", $denipaddress;
 
#use substr to split into octets
my $hexoctet1= substr($hexip, 0, 2);
my $hexoctet2= substr($hexip, 2, 2);
my $hexoctet3= substr($hexip, 4, 2);
my $hexoctet4= substr($hexip, 6, 2);
 
my $denoctet1= hex($hexoctet1);
my $denoctet2= hex($hexoctet2);
my $denoctet3= hex($hexoctet3);
my $denoctet4= hex($hexoctet4);
 
my $ipaddress="$denoctet1.$denoctet2.$denoctet3.$denoctet4" ;
 
return $ipaddress;
}
 
 
 
MAIN:
#start of body of program
{
#set starting ipaddress & no. of hosts to count through.
my $ip="192.168.1.0";
my $blocksize="1024";
print "start ipaddress is $ip, block size is $blocksize. \n";
 
my @ipaddress = &IP2DENARY($ip);
my $denipaddress=$ipaddress[1];
 
for ($count=$denipaddress; $count<($denipaddress+$blocksize); $count++)
{
my $ipaddress2=DENARY2IP($count);
print "ipaddress is $ipaddress2 \n";
}
 
 
 
 
 
#my @ipaddress = &IP2DENARY($ip);
#my $hexipaddress=$ipaddress[0];
#my $denipaddress=$ipaddress[1];
 
 
#my $ipaddress2=DENARY2IP($denipaddress);
 
 
 
}
exit (0);
 
rb/perl-to-update-riunning-total.txt · Last modified: 13/09/2023 12:16 by andrew