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

I get 500 errors all the time when I am writing cgi but after soem debuging i always figure it out. but with this one i can find no explination to the error and have looked it over and over and over. if you could hel it would be greatly apiciated. so here it is

!/usr/bin/perl use CGI; my $query = CGI->new(); my $name = $query->cookie('username'); my $name2 = param('nametext'); open FILE, "user/$name" or die "Error: $!\n"; my %data = (); while ( my $line = <FILE> ) { chomp $line; my ($key, $value) = split(/\s*:\s*/, $line); $data{$key} = $value; } close FILE; open FILE2, "user/$name2" or die "Error: $!\n"; my %data2 = (); while ( my $line2 = <FILE> ) { chomp $line2; my ($key, $value) = split(/\s*:\s*/, $line2); $data2{$key} = $value; } close FILE2; print "content-type: text/html\n\n"; print "<html>"; print "<head><title>!BATTLE!</title></head>"; print "<body bgcolor=black text=silver>"; print "<p>And The Feirce battle begins!"; sub genRand { my $startNumber = shift(); my $endNumber = shift(); my $randNumber = int($startNumber + rand() * ($endNumber - $startNumbe +r)); return $randNumber; }; $hp=$data{htp}; $minatt=$data{minatt}; $maxatt=$data{maxatt}; $hp2=$data2{htp}; $minatt2=$data2{minatt}; $maxatt2=$data2{maxatt}; while ($hp > 0 and $hp2 > 0) { $att=genRand($minatt,$maxatt); print "<p> you deal ",$att," damage! he has "; $hp2=$hp2-$att; print $hp2," HP remaining"; $att2=genRand($minatt2,$maxatt2); print "<p> he deals ",$att2," Damage!"; $hp=$hp-$att2; print " you have ",$hp," HP remaining"; if ($hp < 1) {print "<p>$name2 Wins!!\n";} elsif ($hp2 < 1) {print "<p> You Win!!\n"; }; };

Replies are listed 'Best First'.
Re: 500 server error with no explenation
by wog (Curate) on Jul 14, 2001 at 08:43 UTC
    Others have pointed out the usefullness of CGI::Carp so you can see some errors that would otherwise be sent to the error log, such as those generated by your dies (update: and your typo as pointed out by eejack, which fortunately prevents the next paragraph from yet being a problem.) (And the error log is a good place to check.)

    That said, your code has a very serious security flaw -- you do not untaint $name and $name2, allowing a string like ;rm -rf / | to be passed to it, resulting in your CGI deleting every file on the server that it can. Or worse is possible. You might be better off somehow encoding filenames of users so that special characters won't be so much of a problem.

    You aren't using warnings and strict which is a very good idea. Please do so. And for your CGI script, turning on taint checking is also a good idea.

    Also, the CGI module has a header method. Why not use it?

    Your script, as shown here, outputs malformed HTML. Best to add a print to close those tags.

Re: 500 server error with no explenation
by synapse0 (Pilgrim) on Jul 14, 2001 at 08:35 UTC
    one thing, when dealing with debugging 500's, always try running the script from the cmd line and make sure you get the output you were looking for.. CGI::Carp helps as well, but it's a good habit to test your code from the cmd line.. CGI.pm allows you to do this, and asks for query parameters before running the code.. just enter your query string and hit ctrl-D to run the script. It will usually tell you what went wrong.
    You can also check the error log files, but that tends to be more cumbersome.. (if you can work with more than one terminal, use a different terminal to tail -f the error log).
    -Syn0
Re: 500 server error with no explenation
by jepri (Parson) on Jul 14, 2001 at 15:31 UTC
    In addition to all the excellent commments above, life is much easier prgramming CGI when all your scripts start like this:

    #!/usr/bin/perl -w #Any CGI coding needs this line, uncomment when the script stops worki +ng #BEGIN { $| = 1; open(STDERR, ">&STDOUT"); print "Content-Type: text/p +lain\n\n Text/plain debug header activiated\n"; } use strict; use diagnostics; use CGI; #use CGI::Debug;

    Credit where credit is due, I learned all those tricks off other people.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: 500 server error with no explenation
by Zaxo (Archbishop) on Jul 14, 2001 at 08:26 UTC
    -!/usr/bin/perl +#!/usr/bin/perl -w

    After Compline,
    Zaxo

      ya that was just a copy/paste error but i should add the -w

      thanks

Re: 500 server error with no explenation
by LD2 (Curate) on Jul 14, 2001 at 08:38 UTC
    This may save you some time and help - try using CGI::Carp. As it states in this node.. add this to your code:

    use CGI::Carp qw(fatalsToBrowser);
Re: 500 server error with no explenation
by eejack (Hermit) on Jul 14, 2001 at 08:43 UTC
    You have a typo...easy one to miss,
    .... my $query = CGI->new(); my $name = $query->cookie('username'); # was my $name2 = param('nametext'); my $name2 = $query->param('nametext'); ....
    Update and all of the above kind advice from my fellow monks was better said by them than I could have said it (and faster too). EEjack
Re: 500 server error with no explenation
by Cobo (Scribe) on Jul 14, 2001 at 08:27 UTC
    Sorry I posted this part before I saw the first reply (must of replied as I was writing). Well the first thing I noticed was !/usr/bin/perl that should read #!/usr/bin/perl Try using strict as well, UPDATE: I found the problem, I switched it to use CGI ':standard'; and it seems to run fine from the command prompt, with the exception of not finding the files, Good Luck
Re: 500 server error with no explenation
by Cobo (Scribe) on Jul 14, 2001 at 09:45 UTC
    Oh and one other thing, I just noticed this in your code you use FILE where it appears you meant to use FILE2,
    open FILE2, "user/$name2" or die "Error: $!\n"; my %data2 = (); while ( my $line2 = <FILE> ) { chomp $line2; my ($key, $value) = split(/\s*:\s*/, $line2); $data2{$key} = $value; } close FILE2;
    Hope I helped
Re: 500 server error with no explenation
by JBowes (Novice) on Jul 14, 2001 at 08:59 UTC
    a change from use CGI; to use CGI ':standard'; fixed the 500 error. why?

      You are using the function interface instead of $query->method

      After Compline,
      Zaxo