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

I don't know what is wrong with this code, it just won't compile.
if($ENV {"REQUEST_METHOD"} eq 'GET') { $rqst = $ENV{"QUERY_STRING"}} else {read(STDIN, &rqst, $ENV{"CONTENT_LENGTH"})} @rqstedtd = split(/&/, $rqst, 2); @user = split(/=/, $rqstedtd[1], 2); @pass = split(/=/, $rqstedtd[3], 2); open(PASS, "pass") or die "Couldn't find password file.\n"; open(LOGG, ">>log") or die "Coundn't find log file.\n"; while(<PASS>) { $fstln = $_; chomp($fstln); @login = split(/\t/, $fstln, 2); unless ($user[1] ne $login[0]) { next; } if($pass[1] eq $login[1]) { print <<EndOfTop Content-type: text/html <html> <title> Login sucessful! <head> <hr> </head> <body bgcolor="black" text="red"> <h3> Login succesful! <br> <br> <a href="www.eircom.net"> <h2> Click here to continue </a> <hr> </body> </html> EndOfTop } else { print <<EndOfBotom <html> <title> Login Unsuccesful </title> <head> <hr> <h3> Login unsuccesful </head> <body bgcolor="black" text="red"> Invalid username\password. <br> Please try again.<br> <br> <hr> <form method=POST action="file://C:\perl\login\password\login. +cgi"> User Name: <input name=user size=30> <br> Password: <input name=pass size=30> <br> <input type="submit" value="Send"> <hr> </body> </html> EndOfBotom }}

edited: Sun Dec 22 22:55:22 2002 by jeffa - title change (was: Help please!!)

Replies are listed 'Best First'.
Re: Problem with CGI script
by pfaut (Priest) on Dec 22, 2002 at 21:50 UTC

    Turn on warnings and use strict. It will make your life a lot easier trying to figure out what's wrong with your perl code.

    If you look in your web server's error log, you'll probably see something about your script not putting out required headers. Your here documents get output verbatim. That includes leading white space. \tContent-type: text/html is not the same is Content-type: text/html. Put that text flush to the left margin.

    Also, most if the work of parsing request information has been done for you. use CGI to take advantage of it instead of trying to roll your own. It's very difficult to get it all right.

    Update: fixed backslash in second paragraph.

    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Problem with CGI script
by dws (Chancellor) on Dec 22, 2002 at 23:09 UTC
    One problem:
    {read(STDIN, &rqst, $ENV{"CONTENT_LENGTH"})} ^--- Perl is not C. This should be $rqst
    The bigger problem is that you're rolling your own form support instead of using CGI.pm

Re: Problem with CGI script
by Beatnik (Parson) on Dec 22, 2002 at 22:56 UTC
    1. use CGI; #CGI.pm on CPAN
    2. <form method=POST action="file://C:\perl\login\password\login.cgi"> is not a valid web location.
    3. The extra newlines before the actual header might b0rk some browsers
    Greetz
    Beatnik
    .. Quidquid perl dictum sit, altum viditur.
Re: Problem with CGI script
by poj (Abbot) on Dec 23, 2002 at 00:12 UTC
    Your code syntax checks out OK but there are lots of errors, many in the HTML.
    This code @rqstedtd = split(/&/, $rqst, 2); limits the split to 2 so $rqstedtd[3] is undefined.
    This rewrite is untested but might help you a bit. Using CGI.pm makes life a lot easier !
    use strict; use warnings; use CGI; my $q = new CGI; my $user = $q->param('user'); my $password = $q->param('password'); # authenticate my ($title,$body); if ( &auth_user($user,$password) eq "OK" ){ ($title,$body) = &validUser(); } else { ($title,$body) = &invalidUser(); }; # print html page print qq(Content-type: text/html\n <html> <head><title>$title</title></head> $body </html>); # # == end of main program # # # authenticate user/passwords # sub auth_user{ my ($user,$password) = @_; open(PASS, "pass") or die "Couldn't find password file.\n"; while(<PASS>){ chomp; my ($u,$p) = split(/\t/, $_, 2); if ( ($u eq $user) && ($p eq $password) ){ close PASS; return "OK"; } } close PASS; return "notOK"; } # valid sub validUser { my $title = "Login successful !"; my $body = q(<body bgcolor="black" text="red"> <h3>Login succesful!</h3><br><br> <a href="www.eircom.net"><h2>Click here to continue</h2></a><hr> </body>); return ($title,$body); } # invalid sub invalidUser { my $title = "Login unsuccessful !"; my $body = q(<body bgcolor="black" text="red"> Invalid username\password.<br> Please try again.<br><br><hr> <form method=POST action="file://C:\perl\login\password\login.cgi"> User Name: <input name="user" size="30"><br> Password: <input name="pass" size="30"><br> <input type="submit" value="Send"></form><hr> </body>); return ($title,$body); }
    poj