katgirl has asked for the wisdom of the Perl Monks concerning the following question:

I'm probably overlooking something blindingly obvious, but it's nearly hometime and I'm not in the mood!

OK, I'm trying to write a storybook that has pictures attached, my users sign up and get a password, then they can upload their story and an optional picture to the storybook.

Below is the upload script - when I run it, even with no picture uploaded, I get a 500 server error. I've tried running it from the command line, but I don't get any error there. I know it probably looks hopelessly messy, but I keep changing bits to try to find out where I've gone wrong and nothing seems to help. *starts to dribble*

Help me, oh wisdomful monks, before I go insane...

#!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); #place to go when successful $redirect = "http://www.mysite.com/soapopera/start.cgi"; #absolute location of the directory for your images $Data = "/usr/local/www/virtual/username/soapopera/images"; #file containing usernames and passwords $datafile = "passes.txt"; #File extensions to allow your visitors to upload. @good_extensions = ('gif', 'jpg', 'jpeg', 'png'); #maximum file size in Kb $max_size_main = 25; #Gives date as "Weekday, Daynumber Month Year" use POSIX qw(strftime); $time= strftime("%A, %d %B %Y", localtime); use CGI; my $req = new CGI; my $name = $req->param("name"); my $password = $req->param("password"); my $title = $req->param("title"); my $story = $req->param("story"); my $storyid = $req->param("storyid"); my $image = $req->param("image"); my $next1 = $req->param("next1"); my $next2 = $req->param("next2"); my $chapter = $req->param("chapter"); my $imagename = $image; $story =~ s/\n/<br>/gi; open(PASSFILE, $datafile) or die $!; @data = <PASSFILE>; close(PASSFILE); foreach $data(@data){ @eachinfo = split(/\|/, $data); if ($eachinfo[0] eq $name && $eachinfo[2] eq $password){ $success = 1; } } if (!$success){ print "<h1>Sorry</h1>That Name/Password combination is not correct. Pr +ess your back button and try again."; } if($success){ if($image) { $imagename =~ s/^.*(\\|\/)//; $imagename =~ s/ +/\_/g; $num=""; while(-e "$Data/$num$imagename"){ $num++; } $imagename = $num.$imagename; my $proceed_type = 0; foreach(@good_extensions) { my $ext = $_; $ext =~ s/\.//g; if($imagename =~ /\.$ext$/) { $proceed_type = 1; last; } } unless($proceed_type) { push(@was_not_good_type, $imagename); } if($proceed_type) { if(open(OUTFILE, ">$Data/$imagename")) { while (my $bytesread = read($image, my $buffer, 1024)) { print OUTFILE + $buffer; } close (OUTFILE); push(@file_did_save, $imagename); } else { push(@did_not_save, $imagename); } } if($max_size_main) { if((-s "$Data/$imagename") > ($max_size_main * 1024)) { push(@was_too_big, $imagename); unlink("$Data/$imagename"); } } #close if file exists... if(@file_did_save){ open (TXTFILE, ">>data/$storyid.txt") or die $!; print TXTFILE "$chapter|$name|$title|$story|$next1|$next2|$imagename|$ +time\n"; close(TXTFILE); print $req->redirect($redirect); } else{ if(@was_not_good_type){ @reason = @was_not_good_type;} if(@was_too_big){@reason = @was_too_big;} if(@did_not_save){@reason = @did_not_save;} print "Content-type: text/html\n\n"; print "An error occured: @reason"; exit; } }else{ #no pic uploaded open (TXTFILE, ">>data/$storyid.txt") or die $!; print TXTFILE "$chapter|$name|$title|$story|$next1|$next2||$time\n"; close(TXTFILE); print $req->redirect($redirect); } }#successful password

Replies are listed 'Best First'.
Re: Can't find where 500 error is coming from
by fglock (Vicar) on Sep 06, 2002 at 16:13 UTC

    I'd place  print "Content-type: text/plain\n\n" in the beginning. This way you can see what's happening.

    It might be dieing before you can see any output.

    update: $datafile should use an absolute filename (  "/dir/file" style).

•Re: Can't find where 500 error is coming from
by merlyn (Sage) on Sep 06, 2002 at 16:30 UTC
    I don't even try to diagnose a 500 error until I can see what happens to the web log. The "real" error message is placed there.

    So, what's in the error log?

    -- Randal L. Schwartz, Perl hacker

Re: Can't find where 500 error is coming from
by theorbtwo (Prior) on Sep 06, 2002 at 16:36 UTC

    I don't immedetly see your problem on a first readthrough, while tried. I agree with fglock about printing your HTTP header as early as possible. Since the header information actualy changes for you depending on processing, that's fairly late, so just stick a Content-Type: text/plain in temporarly so you can see what's going on.

    I suspect, however, that what's going on is that an open is failing, somthing you don't test for nearly enough. Whenever you say "open", an "or die "...: $!" should fly off of your fingers like second nature. I made that mistake with my first post here, and got back a million and a half replies yelling at me for it -- and it was good advice, but most people seemed to ignore the actual problem I was having. (Yes, that was mostly completly irrelevant. I'm in a retrospective mood.)

    Oh, and another note -- you should never store passwords in plaintext like that. Not only is it a security risk, but at some point you'll end up with passwords in your head that have no busniess being there, and will just tempt you -- or at least clutter up memory space you could be using for more useful things.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by responing to this node).

Re: Can't find where 500 error is coming from
by katgirl (Hermit) on Sep 09, 2002 at 08:05 UTC
    *Puts on hair shirt and self-flagellates*

    Thank you oh monks for helping me see the error of my ways!

    I've now found the problem and fixed it - I had forgotten to remove the newline from the passwords before checking to see if they were correct, so it was always coming up with password incorrect, and I also forgot to put in Content type where the error message for wrong password was. All fixed, and I'm happy :)