Question

I am getting error as "Internal Server Error.The server encountered an internal error or misconfiguration and was unable to complete your request."

I am submitting a form in html and get its values.

HTML Code (index.cgi)

#!c:/perl/bin/perl.exe
print "Content-type: text/html; charset=iso-8859-1\n\n";
print "<html>";
print "<body>";
print "<form name = 'login' method = 'get' action = '/cgi-bin/login.pl'> <input type = 'text' name = 'uid'><br /><input type = 'text' name = 'pass'><br /><input type = 'submit'>";
print "</body>";
print "</html>";

Perl Code to fetch data (login.pl)

#!c:/perl/bin/perl.exe
use CGI::Carp qw(fatalsToBrowser);
my(%frmfields);
getdata(\%frmfields);
sub getdata {
    my ($buffer) = "";
    if (($ENV{'REQUEST_METHOD'} eq 'GET')) {
        my (%hashref) = shift;
        $buffer = $ENV{'QUERY_STRING'};
        foreach  (split(/&/,$buffer)) {
            my ($key, $value) = split(/=/, $_);
            $key = decodeURL($key);
            $value= decodeURL($value);
            $hashref{$key} = $value;
        }
    }
    else{
        read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'})
    }
}

sub decodeURL{
    $_=shift;
    tr/+/ /;
    s/%(..)/pack('c', hex($1))/eg;
    return($_);
}

The HTML page opens correctly but when i submit the form, i get internal server error.

Please help.

Was it helpful?

Solution

What does the web server's error log say?

Independent of what it says, you must stop parsing the form data yourself. There are modules for that, specifically CGI.pm. Using that, you can do this instead:

use CGI;

my $CGI = CGI->new();

my $uid  = $CGI->param( 'uid'  );
my $pass = $CGI->param( 'pass' );

# rest of your script

Much cleaner and much safer.

OTHER TIPS

I agree with Tore that you must not parse this yourself. Your code has multiple errors. You don't allow multiple parameter values, you don't allow the ; alternate separator, you don't handle POST with a query string in the URL, and so on.

I don't know how long it will be online for free, but chapter 15 of my new "Beginning Perl" book covers Web programming. That should get you started on some decent basics. Note that the online version is an early, rough draft. The actual book also includes Chapter 19 which has a complete Web app example.

could it be this line that's the problem?

my (%hashref) = shift;

You're initialising a proper hash, but shift will give you a hash reference, since you did getdata(\%frmfields);. You probably want this, instead:

my $hashref = shift;

"500 Internal Server Error" just means that something didn't work the way the web server expected. Maybe you don't have CGI enabled. Maybe the script isn't executable. Maybe it's in a directory the web server isn't allowed to access. It's even possible that maybe the web server ran the script successfully and it worked perfectly, but didn't start its output with a valid set of HTTP headers. You need to look in the web server's error log to find out what it didn't like, which may or may not be a Perl issue.

Like everyone else has said, though, don't try to parse query strings and grovel though %ENV yourself. Use one of the many fine modules or frameworks which are available and already known to work correctly. CGI.pm is the granddaddy of them all and works well for smaller projects, but I'd recommend looking into a proper web application framework such as Dancer, Mojolicious, or Catalyst (there are many others, but those are the big three) if you're planning to build anything with more than a handful of relatively simple pages and forms.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top