This is my first attempt at writing an actual CGI script, and I would like any comments or suggestions as to how to make this script more secure, since I would like to put this on my website, which is on
jcwren's server.
This script is supposed to keep track of the number of downloads of two different files. If you click the link, it should update a text file, then redirect to the actual file to be downloaded.
#!/usr/bin/perl -Tw
use strict;
use CGI qw(:all);
my $CGI = new CGI;
my $file;
my $countfile="downloadcounts.txt";
my $OS_Type=$CGI->param("OS");
my %OS_Count=();
my $OS_Name;
my $count;
open(COUNT,"$countfile")||die"Can't open: $!\n";
while(<COUNT>){
($OS_Name,$count)=split /=/;
chomp($OS_Name);
chomp($count);
$OS_Count{$OS_Name}=$count;
}
close COUNT;
if($OS_Type=~/Linux/ || $OS_Type=~/Windows/){
if(exists $OS_Count{$OS_Type}){
$OS_Count{$OS_Type}++;
}
&write(\%OS_Count);
if($OS_Type=~/Windows/){
$file="http://tstanley.perlmonk.org/QuizTaker32-V108.zip";
$CGI->redirect($file);
}elsif($OS_Type=~/Linux/){
$file="http://tstanley.perlmonk.org/QuizTaker-V1.08.tar.gz";
$CGI->redirect($file);
}else{
print $CGI->header();
print $CGI->start_html('Try Again!');
print $CGI->h1(-align=>'center','Incorrect OS!!');
print $CGI->end_html();
}
}elsif($OS_Type=~/Show/){
print $CGI->header();
print $CGI->start_html('Number of Downloads');
foreach my $key(keys %OS_Count){
print $CGI->h3("$key = $OS_Count{$key} downloads");
}
print $CGI->end_html();
}
sub write{
my $Hash=shift;
open(COUNT,">$countfile")||die"Can't open: $!\n";
foreach my $key (keys %$Hash){
print COUNT "$key=$$Hash{$key}\n";
}
close COUNT;
}
TStanley
--------
There's an infinite number of monkeys outside who want to talk to us
about this script for Hamlet they've worked out
-- Douglas Adams/Hitchhiker's Guide to the Galaxy
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.