PDA

View Full Version : My first day


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

KevinADC
04-14-2006, 12:28 AM
You're saying you wrote that script from scratch the very first day you started learning perl? Forgive me if I am sceptical, not that it's the work of what I would associate with an experienced perl programmer, but it's definetly way beyond what a typical person would code after one day of self instruction.

As far as the code goes, it's OK. No major problems. Could be a bit more compact but nothing to worry about really.

tompierce
04-14-2006, 01:09 PM
Well, the first 10lines are pretty much straight from a cgi101.com online tutorial book , where i learnt all the basics from(i read the 6 chapters they have available quite thoroughly)
And i'm not new to web design or programming, so alot of it seemed like common sense to me.
Thanks for checking over the code though, its good to see i'm not going badly wrong anywhere.
Tom

mlseim
04-14-2006, 02:34 PM
Waking up this morning ... you know, I don't like Windows, I think I'll
write my own operating system ...

Just kidding ..

It's great you can pick it up that easily.

You can experiment with your scripts on a webhost (your own website)
pretty cheap ...

Try this host: http://www.cleverdot.com

Your investment? About $32.00 for one year, and that includes your
own domain name. It's worth playing around with, and if you decide not
to have your own website, it's only a $32 investment. You can hardly
afford not to have a sandbox to play in.

And you can dabble with Perl, PHP and MySQL .. all at the same time.

tompierce
04-14-2006, 02:47 PM
Cool, i'll look into that. Will have to wait till next pay day now though :o

One question, what does the -T switch do?
When i have:
#!/perl/bin/perl -wT
I get an error(Insecure dependency in open while running with -T switch) on this line:
open(SAVEFILE,">>$savename");

But if i remove the T, it works fine. I'm abit worried by the word "insecure" in the error :/
#edit, did abit of searching, found out it means taint. And treats anything from outside its "world" as tainted. Still not entirely sure how i'd untaint my variable though..
(its just $ENV{QUERY_STRING} in a scalar variable)

KevinADC
04-14-2006, 07:50 PM
Waking up this morning ... you know, I don't like Windows, I think I'll
write my own operating system ...

Just kidding ..

It's great you can pick it up that easily.

You can experiment with your scripts on a webhost (your own website)
pretty cheap ...

Try this host: http://www.cleverdot.com

Your investment? About $32.00 for one year, and that includes your
own domain name. It's worth playing around with, and if you decide not
to have your own website, it's only a $32 investment. You can hardly
afford not to have a sandbox to play in.

And you can dabble with Perl, PHP and MySQL .. all at the same time.


You don't need to spend one cent. All the programs required to run a server on your PC are free. Apache, perl, PHP, mySQL, etc etc. All you need is a PC with an operating system installed and you are ready to go. But if you don't want to do that (not sure why you wouldn't though) you can certainly use low cost webhosting as your sandbox.

KevinADC
04-14-2006, 07:54 PM
One question, what does the -T switch do?


http://search.cpan.org/dist/perl/pod/perlsec.pod