tompierce
04-13-2006, 09:55 PM
I woke up this morning and decided i wanted to learn something new, read abit about web databases then came across cgi and perl. I think its great!
Its now 21:45 and i'm pretty pleased with what i've accomplished.
Eventually i'd like to have my own website, based on my major intrest, photography. So after learning the basics, i thought i'd try and code an image gallery using cgi and perl, and it all seems to work just how i want it to. :)
I was just wondering if someone could check over my code, to see where i've made unneccessary errors or similar things.
Also:
Is this even a good way of doing an image gallery?
Is it secure?
This is my main ImageGallery.cgi file.
It opens images2.txt(just a list of urls) and loads all the images.
It sets a limit on the number of pictures per page and displays them.
#!/perl/bin/perl -wT
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
open(FH,"images2.txt") or &dienice("Cant open images2.txt: $!");
my @ary = <FH>;
close(FH);
print header;
print start_html("Gallery");
my $rowlimit = 0;
my $page = $ENV{QUERY_STRING};
my $shotsperpage = 2;
my $totalimages = @ary;
my $totalpages = $totalimages/$shotsperpage;
if ($page > 0)
{
#nothing
}
else{
$page = 1;
}
print "<center><H1>My Gallery</H1>";
print "(Click on an image to enlarge)<br>";
my $i = 0;
for ($i = 0; $i < $totalpages; $i++){
my $iplus = $i + 1;
if ($page == $iplus){
print "| Page $iplus |";
}
else{
print qq(| <A href="ImageGallery.cgi?$iplus">Page $iplus</A> |);
}
}
print "<br>";
for ($i = 0; $i < $totalimages; $i++){
my $limit = $page*$shotsperpage-$shotsperpage;
my $otherlimit = $page*$shotsperpage;
if ($i >= $limit && $i < $otherlimit ){
my $line = @ary[$i];
print qq(<a href="fullsizeImage.cgi?$line"<img src="$line" border="0" vspace="10" hspace="10" height="100"
width="100"></a>);
$rowlimit+=1;
if ($rowlimit >= 3){
print "<br>";
$rowlimit = 0;
}
}
}
print "</center>";
print end_html;
sub dienice {
my($msg) = @_;
print header;
print start_html("Error");
print h2("Error");
print $msg;
print end_html;
exit;
}
I also use fullsizeImage.cgi to display an image fullsize once its been clicked.
#!/perl/bin/perl -wT
#Use standard cgi function library thing
use CGI qw(:standard);
#Use the carp error/debugging tool
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
#enforce the declaring all variables thang
use strict;
print header;
print start_html;
print qq(<a href="ImageGallery.cgi">Back to Gallery</A><br>);
print qq(<img src="$ENV{QUERY_STRING}" hspace="10" vspace="10"><br>Copyright Tom Pierce 2006);
print qq(<a href="ImageGallery.cgi"><br>Back to Gallery</A><br>);
print end_html;
I haven't accquired a webhost yet, so unfortunately i can't show an example, or any of my photos :p but i hope that i've provided enough infomation that someone could get it working.
Thanks for your time,
Tom
Its now 21:45 and i'm pretty pleased with what i've accomplished.
Eventually i'd like to have my own website, based on my major intrest, photography. So after learning the basics, i thought i'd try and code an image gallery using cgi and perl, and it all seems to work just how i want it to. :)
I was just wondering if someone could check over my code, to see where i've made unneccessary errors or similar things.
Also:
Is this even a good way of doing an image gallery?
Is it secure?
This is my main ImageGallery.cgi file.
It opens images2.txt(just a list of urls) and loads all the images.
It sets a limit on the number of pictures per page and displays them.
#!/perl/bin/perl -wT
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;
open(FH,"images2.txt") or &dienice("Cant open images2.txt: $!");
my @ary = <FH>;
close(FH);
print header;
print start_html("Gallery");
my $rowlimit = 0;
my $page = $ENV{QUERY_STRING};
my $shotsperpage = 2;
my $totalimages = @ary;
my $totalpages = $totalimages/$shotsperpage;
if ($page > 0)
{
#nothing
}
else{
$page = 1;
}
print "<center><H1>My Gallery</H1>";
print "(Click on an image to enlarge)<br>";
my $i = 0;
for ($i = 0; $i < $totalpages; $i++){
my $iplus = $i + 1;
if ($page == $iplus){
print "| Page $iplus |";
}
else{
print qq(| <A href="ImageGallery.cgi?$iplus">Page $iplus</A> |);
}
}
print "<br>";
for ($i = 0; $i < $totalimages; $i++){
my $limit = $page*$shotsperpage-$shotsperpage;
my $otherlimit = $page*$shotsperpage;
if ($i >= $limit && $i < $otherlimit ){
my $line = @ary[$i];
print qq(<a href="fullsizeImage.cgi?$line"<img src="$line" border="0" vspace="10" hspace="10" height="100"
width="100"></a>);
$rowlimit+=1;
if ($rowlimit >= 3){
print "<br>";
$rowlimit = 0;
}
}
}
print "</center>";
print end_html;
sub dienice {
my($msg) = @_;
print header;
print start_html("Error");
print h2("Error");
print $msg;
print end_html;
exit;
}
I also use fullsizeImage.cgi to display an image fullsize once its been clicked.
#!/perl/bin/perl -wT
#Use standard cgi function library thing
use CGI qw(:standard);
#Use the carp error/debugging tool
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
#enforce the declaring all variables thang
use strict;
print header;
print start_html;
print qq(<a href="ImageGallery.cgi">Back to Gallery</A><br>);
print qq(<img src="$ENV{QUERY_STRING}" hspace="10" vspace="10"><br>Copyright Tom Pierce 2006);
print qq(<a href="ImageGallery.cgi"><br>Back to Gallery</A><br>);
print end_html;
I haven't accquired a webhost yet, so unfortunately i can't show an example, or any of my photos :p but i hope that i've provided enough infomation that someone could get it working.
Thanks for your time,
Tom