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

Hello, it seems i like everyones knowladge alot.. so ill be asking anoter question :)
I need to allow users to login to my website. Ive writen some code using HTML::Template. My understanding of CGI is not execatly very substantial and ive tried to work around this to create a login. Ive put a forms on my page and im using param('user') to get the password once its submitted. Then ive used an if() to see if it been submitted then i will do somthing else but for now its just a debug messege, but this doesn't seem to work. Here is my code
use strict; use HTML::Template; use CGI; my $login = "login <input type=\"text\" name=\"user\"> password <input + type=\"text\" name=\"pass\"> <input type=\"submit\" value=\"Submit\ +" name=\"submit\">"; my $template = HTML::Template->new(filename => "template"); $template->param(LOGIN => $login); ## Put an IF here after finding out + if they have a cookie / ticket :) ## $template->param(NAV => "blah blah"); $template->param(CONTENT => "woof woof"); my $cgi = new CGI; my $user = $cgi->param('user'); my $pass = $cgi->param('pass'); if ($user) { $cgi->start_html; print "Content-Type: text/html\n\n"; print "well HELLO"; $cgi->end_html; } else { $cgi->start_html; print "Content-Type: text/html\n\n", $template->output; $cgi->end_html; }
If anyone could help with this it would rock :)
Thanks
im the king of all i survey. my kingdom has lots of dust on it

Replies are listed 'Best First'.
Re: Simple CGI Login
by blue_cowdawg (Monsignor) on Aug 30, 2006 at 23:40 UTC
        if ($user) { $cgi->start_html; print "Content-Type: text/html\n\n"; print "well HELLO"; $cgi->end_html; } else { $cgi->start_html; print "Content-Type: text/html\n\n", $template->output; $cgi->end_html; }

    I'd be looking to change that to something like this:

    if ($user) { print $cgi->header,$cgi->start_html,"Hi there!", $cgi->end_html; } else { print $cgi->header,$cgi->start_html, $template->output,$cgi->end_html; }
    Printing the content type is handled by the "header" method of the CGI object.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      It is preferable to run print the header that way but it doesn't have to be. The problem is when i put a user/pass into the fields and click submit, nothing happens, unlike i think it should....
      im the king of all i survey. my kingdom has lots of dust on it
            The problem is when i put a user/pass into the fields and click submit, nothing happens, unlike i think it should

        Where in the code you submitted are you processing the data coming back from submission? Where are you starting the form? What does the form look like?


        Peter L. Berghold -- Unix Professional
        Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Simple CGI Login
by thil (Sexton) on Aug 31, 2006 at 06:59 UTC
    What is your Form look like? and what do you mean by 'it seems not wroking' ?
    I am a newbie to Perl, so I don't know these tips are really useful for you :). anyways, let me give my view points as well.

    how do your form knows where it should pass its Post data once a username and password is entered and hits Submit?
    and have you tried before to get the Post data through CGI and were you able to get it?
    and how about doing it this way?
    #!/path/ use strict; use CGI; my $page = new CGI; my $name =$page->param('Uname'); my $password=$page->param('Pword'); if($name ne '' and $password ne ''){ # Display the WebPage } else{ # Show loggin Form }

    HTML Loggin Page
    <html> <head></head> <body> <table> <form method=Post action=yourperlscript.pl > <tr> <td>User Name</td> <td><input type=text name='Uname'></td> </tr> <tr> <td>Password</td> <td><input type=password name='Pword'></td> </tr> <tr> <input type=submit name='submit' value='Submit'> </tr> </form> </table> </body> </html>
      I have to say, i totaly forgot about using post to give the infomation to the script, Thanks :)
      im the king of all i survey. my kingdom has lots of dust on it
Re: Simple CGI Login
by Anonymous Monk on Aug 31, 2006 at 11:49 UTC
    Hi Xidus, since authentication is one of the most needed functions in web applications, I wrote the CGI::Session::Auth module. Maybe using that makes the task easier than reinventing that particular wheel? Best regards, Jochen